4 回答
TA贡献217条经验 获得超353个赞
import java.util.Arrays; public class Test5 { public static void main(String[] args) { /* * 编写Java代码把数组 array1 = {1, 31,11, 41,21, 51}的元素 * * 和数组array2 = {53, 13,33,23, 43, 3}的元素放到数组array3中 */ int[] array1 = { 1, 31, 11, 41, 21, 51 }; int[] array2 = { 53, 13, 33, 23, 43, 3 }; int[] array3 = new int[array1.length + array2.length]; for (int i = 0; i < array3.length; i++) { array3[i] = (i < array1.length ? array1[i] : array2[i - array1.length]); } System.out.println("排序前:" + Arrays.toString(array3)); /* 并对数组array3的元素进行升序排序 */ for (int i = 0; i < array3.length - 1; i++) { for (int j = i + 1; j < array3.length; j++) { if (array3[i] > array3[j]) { array3[i] = array3[i] ^ array3[j]; array3[j] = array3[i] ^ array3[j]; array3[i] = array3[i] ^ array3[j]; } } } System.out.println("排序后:" + Arrays.toString(array3)); } }
TA贡献140条经验 获得超197个赞
import java.util.Arrays; public class Test { public static void main(String[] args) { int[] array1 = {1, 31, 11, 41, 21, 51}; int[] array2 = {53, 13, 33, 23, 43, 3}; int[] array3 = new int[array1.length + array2.length]; for (int i = 0; i < array3.length; i++) { array3[i] = i < array1.length ? array1[i] : array2[i - array1.length]; } System.out.println("排序前:" + Arrays.toString(array3)); Arrays.sort(array3); System.out.println("排序后:" + Arrays.toString(array3)); } }
代码以上,望采纳。
TA贡献5条经验 获得超1个赞
public void test3() {
int[] a = { 11, 22, 88, 44, 55 };
int[] b = { 66, 77, 33, 99, 00 };
int[] c = new int[a.length + b.length];
for (int i = 0; i < c.length; i++) {
c[i] = i < a.length ? a[i] : b[i - b.length];
}
Arrays.sort(c);
System.out.println(Arrays.toString(c));
}
TA贡献17条经验 获得超11个赞
import java.util.Arrays;
public class TEXT5 {
public static void main(String[] args) {
int []array1={1,31,11,41,21,51};
int []array2={53,13,33,23,43,3};
int leng = array1.length+array2.length;//两个数组的总长度等于第三个数组的长度
int []array3=new int[leng];
for(int i=0;i<leng;i++){
if (i<array1.length) {//先将array1的值赋值给array3
array3[i]=array1[i];
}
else {//再将array2的值赋值给array3
array3[i]=array2[i-array1.length];
}
}
System.out.println("合并后:"+Arrays.toString(array3));
Arrays.sort(array3);//排序
System.out.println("排序后:"+Arrays.toString(array3));
}
}
添加回答
举报