写一个在数组中插入新数据的代码吧
import java.util.Scanner;
public class Hx {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int [] arr ={5,3,7,6,9,48};
System.out.println("要插入的数字");
int a =sc.nextInt();
System.out.println("插入的位置");
int b= sc.nextInt()-1;
ax(a,b,arr);
}
public static void ax(int a,int b,int[] arr){
int [] brr =new int [arr.length+1];
for (int i=0;i<brr.length;i++){
if (i<b){
brr[i]=arr[i];
}
else if (i==b){
brr[i]=a;
}
else{
brr[i]=arr[i-1];
}
}
for (int i=0;i<brr.length;i++){
System.out.print(brr[i]+" ");
}
}
}
|
|