1、找到x对应数组的角标index,找出第一个比我小的数
2、a[a.length-2]去覆盖a[a.lenhth-1]
3、用x替换位于index的元素;
int[] a = {12, 23, 32, 12, 34};
int x = 24;
int index = -1;
for (int i = 0; i <= a.length; i++) {
if (a[i] > x) {
index = i;
break;
}
}
//覆盖
for (int i = a.length - 2; i >= index; i--) {
a[i + 1] = a[i];
}
//替换
a[index] = x;
//输出
for (int b : a) {
System.out.print(b + " ");
}
}
网友评论