两数组中的元素对应位置互换问题
两个数组array1{1,2,3,4},array2{5,6,7,8}对应位置互换:
public class ArrayHuHuan{
public static void main(String[] args){
int[] array1 = new int[]{1,2,3,4};
int[] array2 = new int[]{5,6,7,8};
int[] array3 = new int[4];
for(int i=0;i<array1.length;i++){
array3[i] = array1[i];
}
for(int i=0;i<array1.length;i++){
array1[i] = array2[i];
}
for(int i=0;i<array1.length;i++){
array2[i] = array3[i];
}
System.out.println("array1:");
for(int v1:array1){
System.out.print(v1+"\t");
}
System.out.println();
System.out.println("array2:");
for(int v2:array2){
System.out.print(v2+"\t");
}
}
}
public class ArrayHuHuan{
public static void main(String[] args){
int[] array1 = new int[]{1,2,3,4};
int[] array2 = new int[]{5,6,7,8};
for(int i=0;i<array1.length;i++){
int x = array1[i];
array1[i] = array2[i];
array2[i] = x;
}
System.out.println("array1:");
for(int v1:array1){
System.out.print(v1+"\t");
}
System.out.println();
System.out.println("array2:");
for(int v2:array2){
System.out.print(v2+"\t");
}
}
}
两个数组array1{1,2,3,4},array2{5,6,7,8,9,0}对应位置互换:
public class ArrayHuHuan{
public static void main(String[] args){
int[] array1 = new int[]{1,2,3,4};
int[] array2 = new int[]{5,6,7,8,9,0};
int[] temp = array1;
array1 = array2;
array2 = temp;
System.out.println("array1:");
for(int v1:array1){
System.out.print(v1+"\t");
}
System.out.println();
System.out.println("array2:");
for(int v2:array2){
System.out.print(v2+"\t");
}
}
}