-
是王小二呀
package test_Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Scanner;
public class ArrayTest {
public static void main(String[] args){
ArrayTest at = new ArrayTest();
at.sortArray();
}
Object sortArray(){
int[] array = new int[]{-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9};
Collection<Integer> arrayList = new ArrayList<Integer>();
System.out.println("Old array's length:"+array.length);
for(int i=0;i<array.length;i++){
System.out.print(" "+array[i]);
arrayList.add(new Integer(array[i]));
}
System.out.print("\n"+"Please input a number:");
arrayList.add(new Integer(input()));
System.out.println("Current array'length:"+arrayList.size());
Object[] now=arrayList.toArray();
Arrays.sort(now);
for(Object currentArray:now){
System.out.print(" "+currentArray);
}
return arrayList;
}
static int input() {
int input = (new Scanner(System.in)).nextInt();
return input;
}
}我在排序之前用了一个匿名类,这时候没有执行排序直接就加入然后输出了,这个才是正确程序,有点儿小区别;不好意思啊。。
-
是王小二呀
package test_Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Scanner;
public class ArrayTest {
public static void main(String[] args){
ArrayTest at = new ArrayTest();
at.sortArray();
}
Object sortArray(){
int[] array = new int[]{-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9};
Collection<Integer> arrayList = new ArrayList<Integer>();
System.out.println("Old array's length:"+array.length);
for(int i=0;i<array.length;i++){
System.out.print(" "+array[i]);
arrayList.add(new Integer(array[i]));
}
System.out.print("\n"+"Please input a number:");
arrayList.add(new Integer(input()));
System.out.println("Current array'length:"+arrayList.size());
Arrays.sort(arrayList.toArray());
for(Object currentArray:arrayList.toArray()){
System.out.print(" "+currentArray);
}
return arrayList;
}
static int input() {
int input = (new Scanner(System.in)).nextInt();
return input;
}
}
-
小律
问题是我不会怎么放到后面
-
嘀咕01
把输入的整数和原理的数字比较,放到小于原数组队后面就好了啊