package com.ww.yzpE;
public class insertArray {
public void insertArray(int[] arr,int index,int value){
for (int j = arr.length - 1; j >=index; j--) { // 为要插入的数留出位置
arr[j] = arr[j - 1];
}
arr[index-1] = value;
}
}
package com.ww.yzpE;
import java.util.Scanner;
public class TestInsert {
/**
* @param args
*/
public static void main(String[] args) {
int number[]=new int[5];
Scanner in = new Scanner (System.in);
System.out.println("请输入依次输入5个整数:");
for (int i = 0; i < number.length; i++) {
number[i]=in.nextInt();
}
System.out.println("插入前:");
for(int i=0;i<number.length;i++){
System.out.print(number[i]+" ");
}
insertArray util = new insertArray();
System.out.println("\n请输入要插入的位置:");
int Position=in.nextInt();
System.out.println("请输入要插入的整数;");
int num=in.nextInt();
util.insertArray(number, Position, num);
System.out.println("\n插入后");
for(int i=0;i<number.length;i++){
System.out.print(number[i]+" ");
}
}
}
转载于:https://blog.51cto.com/765133133/1427435