import java.util.Scanner;
class hello {
public static void main(String[] args) {
Scanner myscanner = new Scanner(System.in);
int[] a = {10, 20, 30, 40, 50, 60, 70, 80};
int x = myscanner.nextInt();
int index = a.length;
for (int i = 0; i < a.length; i++) {
if (x <= a[i])
{ index = i;//找到需要插入的下标i
break;}
}
int b[] = new int[a.length + 1];
for (int i = 0, j = 0; i < b.length; i++) {
if (index != i) {
b[i] = a[j];
j++;
} else
b[i] = x;//将数插入数组
}
a=b;
for (int j = 0; j < a.length; j++)
System.out.print(a[j] + " ");
}
}
java实现升序数组插入数后任然是升序数组
最新推荐文章于 2024-08-22 21:24:01 发布