/** * * @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]+"\t"); } }