# Java基础数组之数组插入

int[] array = {3,7,11,20,35,0};//0此处就是个占位符
Scanner input = new Scanner(System.in);
System.out.print("请输入想插入的数字：");
int t = input.nextInt();
int i = array.length-2;
//挪位
for(;i>=0;i--){
if(array[i]>t){
array[i+1] = array[i];
}else{
break;
}
}
//插入
array[i+1] = t;
for (int s : array) {
System.out.print(s+"\t");
}


int[] array = {3,7,11,20,35,0};//0此处就是个占位符
Scanner input = new Scanner(System.in);
System.out.print("请输入想插入的数字：");
int t = input.nextInt();
int i = 0;
//定位
for (; i < array.length; i++) {
if(array[i]>t){
break;
}
}
//挪
for (int j = array.length-2; j >=i; j--) {
array[j+1]  =  array[j] ;
}
//插
array[i>=array.length?array.length-1:i] = t;
for (int s : array) {
System.out.print(s+"\t");
}

int[] array = {3,10,13,21,55,0};
Scanner input = new Scanner(System.in);
System.out.print("请输入插入的值：");
int t = input.nextInt();
int i = array.length-2;
boolean is  = true;//假设第i个比val大;array[i]>val
for(;i>=0;i--) {
if(array[i]>t){
array[i+1] = array[i];//如果array[i]>val,就挪位
}else{
is = false;
break;
}
}
if(!is||(i==-1)){
array[i+1] = t;
}else{
array[array.length-1] = t;
}
for (int s : array) {
System.out.print(s+"\t");
}

09-28 3万+

10-14 2087

07-16 1万+

08-02 7万+

03-26 2236

11-11 439

04-09 1万+

09-14 14万+

04-13 4万+

08-28 122

06-24 906

01-17 1万+

12-03 6847

08-28 2750

10-04 158

10-24 240

10-21 219

05-06 4709

03-10 2220

05-29 707

04-13 825

09-15 315

04-14 59万+

03-13 15万+

02-19 18万+

03-04 14万+

03-06 3120

03-08 2万+

04-25 7万+

03-10 13万+

03-10 19万+

03-12 11万+

03-13 11万+

03-16 2426

03-18 1万+

03-19 8万+

03-19 3万+

03-20 1万+

03-22 5007

03-23 4万+

03-24 3万+

03-25 3万+

05-08 5万+

03-25 9万+

03-27 5万+

03-29 22万+

03-29 10万+

03-30 17万+

06-01 7047

04-02 6626

04-02 4万+

06-01 616

04-06 1万+

04-06 7万+

04-06 4438

04-09 8万+

04-09 3万+

05-17 9545

04-11 4万+

04-15 6万+

04-18 5万+

04-20 5万+

04-24 3万+

05-03 1万+

05-16 5万+

05-06 2万+

05-06 2422

05-07 4197

05-08 4万+

05-10 3860

05-11 2810

05-14 7272

05-14 1415

05-16 4124

05-16 1万+

05-17 5571

05-30 2087

05-18 2767

05-18 9145

05-18 4198

#### SpringBoot + Vue + Electron 开发 QQ 版聊天工具

©️2019 CSDN 皮肤主题: 游动-白 设计师: 上身试试