/** * * @Title: test_insert_array * @Description: 该方法的主要作用:像数组中插入元素 * @param 设定文件 * @return 返回类型:void * @throws */ @Test public void test_insert_array(){ Scanner scanner = new Scanner(System.in); int [] list = new int[6]; list [0] = 88; list [1] = 92; list [2] = 99; list [3] = 98; list [4] = 54; int index = list.length; //保存新增元素的插入位置 System.out.println("请输入新增成绩:"); int num = scanner.nextInt(); for (int i = 0; i < list.length; i++) { if(num>list[i]){ index = i; break; } } //元素后移 for (int i = list.length-1; i >index; i++) { list[i] = list[i-1]; } list[index] = num; //保存插入的数据 System.out.println("插入数据的下标是:"+index); System.out.println("插入的入数据是:"+num); //进行排序 Arrays.sort(list); for (int i = 0; i < list.length; i++) { System.out.println(list[i]+" "); } }