java array swap

Solutions on MaxInterview for java array swap by the best coders in the world

showing results for - "java array swap"
Sara
12 Mar 2020
1public static void swap(int x, int y, int[] arr) {
2  	int temp = arr[x];
3  	arr[x] = arr[y];
4  	arr[y] = temp;
5}
Emilio
28 Apr 2020
1public static final <T> void swap (T[] a, int i, int j) {
2  T t = a[i];
3  a[i] = a[j];
4  a[j] = t;
5}
6
7public static final <T> void swap (List<T> l, int i, int j) {
8  Collections.<T>swap(l, i, j);
9}
10
11private void test() {
12  String [] a = {"Hello", "Goodbye"};
13  swap(a, 0, 1);
14  System.out.println("a:"+Arrays.toString(a));
15  List<String> l = new ArrayList<String>(Arrays.asList(a));
16  swap(l, 0, 1);
17  System.out.println("l:"+l);
18}
19
Jan
01 Jan 2017
1import java.util.Arrays;
2
3// ... //
4
5int[] digits = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
6
7// Rotate is a fancy way to say split around an index, and swap the chunks
8// For the comments, assume `A=digits`
9int[] rotate(int[] A, int r) {
10  int N = A.length; // total number of elements
11  
12  // Arrays.copyOfRange(array, start, end) will return array[start:end),
13  // where start is inclusive and end exclusive
14  int[] left = Arrays.copyOfRange(A, 0, r); // [0,1,...,r-1], so r=3->[0,1,2]
15  int[] right = Arrays.copyOfRange(A, r, N); // [r,r+1,...,N-1], so r=7->[7,8,9]
16  
17  // Now, concatenate right with left and store in result
18  // - in JS this would be `result=[...right, ...left]`
19  int[] result = new int[N];
20  int R = N - r; // length of right array
21  for(int i=0; i<N; ++i) {
22    // ternary expression: same as
23    //   `if(i<R) result[i] = right[i];`
24    //   `else    result[i] = left[i-R];`
25    result[i] = i<R ? right[i] : left[i-R];
26  }
27  
28  return result;
29}
Larissa
08 Oct 2019
1String temp = arr[1];
2arr[1] = arr[2];
3arr[2] = temp;
4
queries leading to this page
java arraylist swaphow to swap two elemnet in an array in javaswap the values in the indexes of array javaswap elements in array without temp in javaarray swap in javaarrays swap in javaswap function in java for arrayswap within int array javaswapping of two elements in an arraylist javajava swap two array elementsreversing the array with swapping numbers javahow to swap two elements in an array javaswaping array value without third variable javatwo arrays swap in javajava swap integersswap elements in arraylist javajava array swapswap the elements of an arraylist in javahow to swap elements in arraylistjava switch items between arraysswap array elements java different variablesswap function 2 element in array in javahow to swap 2 items in a array javadoes java have any method for swapping elements in an arrayjava swap array elementsinbuilt swap function in java for arrayhow to swap in java using function in arrayswapping of two numbers in array in javaswap adjacent elements in array javaget the new ordered arraylist after swapjava 8 swap elements in arraylistcollections switch to index javahow to swap elements of an array in javaarray swap javaswap the array elements in javahow to swap values in array javajava 8 swap elements in listswap array function javaswap chunks of array in javaswap two array elements javaswap in javaarray swaping in javahow to swap numbers in an array javaswap in java array elementsjava arrays swapswapping of two arrays in javahow to swap elements in an array javaswap two elements in array javahow to swap values in an array javaswap int array javaswap positions in java arrayswap element from beginning of array javaswap elements of two array javajava swap array methodhow to switch between cells in an array of strings in javaswap positions array javaswapping index in list javalist swap javahow to swap to number in sorted array in javaswapping numbers in array javaswap in arraylistarray swap 2c javaswap two array values javajava how to swap elements in an arraymethod to swap to elements in array javaswapping of array elements in javajava arrayswapswap elements array javajava switch items in an arrayhow to swap 4 element arrays in javajava switch case for int array 5cswap array elements in javaswapping two numbers in array in javaswap items inside sub array javaarray swap javahow to swap integers in an array javaexchange elemets in arraylistswap function java arraywhy doesn 27t temp swap work on arrays javahow to swap 2 eleements ona arrayin javahow to swap array elements in javajava switch array variableswap using array in javahow to swap two array elements in javaswap values in array javaswap 2 int in array javaarray java swap numbersbest way to swap elements java arrayjava swap arrayswap string c 2b 2bswapping two elements in an array javaswap array method in javahow to swap 4 element arrays in java exampleswap function 2 element in list in javaswap elements in array jabaswap two elements in an array javaswap elements in array javaswap array javaswap two numbers in array using function in javaswap method in arrays javajava swapping elements in an arrayswap array index by index javaswap array index javajava swap ints in listjava switch two items in an arrayjava swap arraylistarray swap in java inbuilthow to swap arry in javaswap parts of an array javaswap function java for arrayhow to switch two values in a list javaswap to arrays for being similar javajava arrays swapswap elements in an array javajava swap elements in arrayswap java arrayswap whole array in javaswap in java function in arrykth element swap in java in arrayarrayutils java swapjava swap 2 array elementsswap elements of an array javaswap element in array javajava switch statement with array value swap them a number of array in javaswapping array elements using temp variable javaswap the index element in an array in javajava array swap methodswap elements of list in javaswapping elements in an array javaswapping two array elements in javajava switch element in arrayarraylist swapswap arraylist elements javajava program to sort an array by swaphow to swap arraylist elements in javaarraylist swap caluesswap 2 elemenet in array in javareverse array java using swapswap method java arrayswap in java arrayhow to swap 2 eleements ona list in javaswap elements of an arraylistswap two item of arraylist in javaswap two indices of array javaarray swap two elements javaswap 2 elements in array javaswpa an element in an arraylist javaswap elements in a list javajava swap in arrayswap the elements of an array in javajava array swap methodhow to swap in list in javajava swich element of arrayswap 2 numbers in array javahow to swap elements in a array javajava switch items in arrayswap 2 elements in an array javaswap elements in list in javaswap function for an array javaswap array in javaswap elements of array in javaswap arraylist javaswap between two arraylist elemet in java reversing the array with swapping javaswap elements of a list function javajava array swap elementsswap in array javajava swap two elements in arrayjava array swapswap array elements java without third variableswitch the numbers inside the array from left to right javaswapping in arraylistjava integer array swapjava swap arraysjava swap entire array methodarray swap elements javaswap array elements javajava swap two values in arrayhow to swap positions in an array javawrite function to swep two item of arraylist in javaswap 2 elements in an arraylisthow to swap elements in an array in javaswapping array elements in javaan array swapp two elements using java 3flist swap method javahow to swap 2 arrys in javaswap function for array in javahow to exchange two numbers in an array in javaswap methode array javaswapping two arrays in javahow to swap an array in javaswapping of two numbers in java using arrayarraylist switch elementsswap an array in javaswap int array java 8swap javaswap function in java arrayhow to swap two arrays in javajava array swap