Te damos la bienvenida a nuestra comunidad, en este lugar hallarás la respuesta que buscabas.
Ejemplo 1: java array intercambio
publicstaticvoidswap(int x,int y,int[] arr)int temp = arr[x];
arr[x]= arr[y];
arr[y]= temp;
Ejemplo 2: array.swap java
publicstaticfinal<T>void swap (T[] a,int i,int j)T t = a[i];
a[i]= a[j];
a[j]= t;publicstaticfinal<T>void swap (List<T> l,int i,int j)Collections.<T>swap(l, i, j);privatevoidtest()String[] a ="Hello","Goodbye";swap(a,0,1);System.out.println("a:"+Arrays.toString(a));List<String> l =newArrayList<String>(Arrays.asList(a));swap(l,0,1);System.out.println("l:"+l);
Ejemplo 3: intercambiar trozos de array en java
importjava.util.Arrays;// ... //int[] digits =0,1,2,3,4,5,6,7,8,9;// Rotate is a fancy way to say split around an index, and swap the chunks// For the comments, assume `A=digits`int[]rotate(int[]A,int r)intN=A.length;// total number of elements// Arrays.copyOfRange(array, start, end) will return array[start:end),// where start is inclusive and end exclusiveint[] left =Arrays.copyOfRange(A,0, r);// [0,1,...,r-1], so r=3->[0,1,2]int[] right =Arrays.copyOfRange(A, r,N);// [r,r+1,...,N-1], so r=7->[7,8,9]// Now, concatenate right with left and store in result// - in JS this would be `result=[...right, ...left]`int[] result =newint[N];intR=N- r;// length of right arrayfor(int i=0; i<N;++i)// ternary expression: same as// `if(i// `else result[i] = left[i-R];`
result[i]= i<R? right[i]: left[i-R];return result;
Ejemplo 4: array.swap java
String temp = arr[1];
arr[1]= arr[2];
arr[2]= temp;
Comentarios y puntuaciones
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)