Skip to content

Instantly share code, notes, and snippets.

@Yur-ok
Created December 16, 2015 14:55
Show Gist options
  • Save Yur-ok/649c3415118829c75072 to your computer and use it in GitHub Desktop.
Save Yur-ok/649c3415118829c75072 to your computer and use it in GitHub Desktop.
package Lesson3.KeyPoint1;
import java.util.Arrays;
/**
* Created by Юрий on 16.12.2015.
*/
public class SwapMethod {
public static void main(String[] args) {
int[] data = {1, 3, 4, 5};
int[] data1 = null;
System.out.println(Arrays.toString(data));
swap(data1, 0, 3);
swap(data, 3, 2);
System.out.println(Arrays.toString(data));
System.out.println(Arrays.toString(data1));
}
static void swap(int[] data, int i, int k) {
int temp;
if ((data != null) && (i < data.length && k < data.length)) {
temp = data[i];
data[i] = data[k];
data[k] = temp;
}
}
}
@liuiv15
Copy link

liuiv15 commented Dec 24, 2015

А если индексы будут отрицательные?

@Yur-ok
Copy link
Author

Yur-ok commented Dec 30, 2015

Ой! Действительно, этого моя программа не учитывает.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment