所以我的任务是从用户那里获取两个数组的数据,然后将这两个数组合并为一个数组,然后将其从最小到最大排序。我有一切正常工作,除了对组合阵列进行排序,我不知道该去哪里。排序数组的最佳方法是什么?
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("please enter the length of list one");
int dex1 = input.nextInt();
int[] list1 = new int [dex1];
System.out.println("Please enter the values for list one");
for ( int i = 0; i < list1.length; i++) {
list1[i] = input.nextInt();
}
System.out.println("please enter the length of list two");
int dex2 = input.nextInt();
int[] list2 = new int [dex2];
System.out.println("Please enter the values for list two ");
for ( int i = 0; i < list2.length; i++) {
list2[i] = input.nextInt();
}
System.out.println("");
System.out.print("list1: ");
printArray(list1);
System.out.println("\n");
System.out.print("list2: ");
printArray(list2);
System.out.println("\n");
int[] list3 = merge(list1, list2);
printArray(list3);
}
public static int [] merge(int[] list1, int[] list2) {
int[] list3 = new int[list1.length + list2.length];
System.arraycopy(list1, 0, list3, 0, list1.length);
System.arraycopy(list2, 0, list3, 5, list2.length);
return list3;
}
public static void printArray(int arr[]){
int n = arr.length;
for( int i = 0; i < n; i++ ) {
System.out.print(arr[i] + " ");
}
}
}
答案 0 :(得分:0)
如果要按正常的加入顺序对数组进行排序。
Arrays.sort(your_array);
如果要执行特殊类型排序,请使用比较器。
Arrays.sort(your_array,your_comparator);