有一个升序的数组,要求插入一个元素,该数组顺序依然是升序。该数组{10,12,40,70}
package input.java;
import java.util.Scanner;
public class lizi2 {
public static void main(String[] args){
int temp = 0;
int arr[] = {10,12,40,70};
int arrNew[] = new int[arr.length+1];
Scanner myScanner = new Scanner(System.in);
for (int i = 0; i < arr.length; i++){
arrNew[i] = arr[i];
}
System.out.println("输入一个数字,插入该数组里面");
int addNum = myScanner.nextInt();
arrNew[arrNew.length-1] = addNum;
arr = arrNew;
for (int i = 0; i < arr.length - 1; i++){
for (int j = 0; j < arr.length - 1 -i; j++){
if (arr[j] > arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
System.out.println("输出该数组");
for (int i = 0; i < arr.length; i++){
System.out.print(arr[i]+"\t");
}
}
}