class HomeWork8
{
public static void main(String[] args)
{
{
System.out.println(d);
}
c.insert(c.a, 4, 20);
System.out.println(e);
}
}
}
class Insert
{
int[] a = {1,2,3,4,5,6,7};
int[] b = new int[a.length + 1];
{
b[i] = a[i];
{
b[j] = a[j - 1];
}
}
{
public static void main(String[] args)
{
Insert c = new Insert();
//遍历没插入之前的数组
for(int d:c.a ){
System.out.println(d);
}
c.insert(c.a, 4, 20);
System.out.println("---------------------");
//遍历插入之后的数组
for(int e: c.b){System.out.println(e);
}
}
}
class Insert
{
int[] a = {1,2,3,4,5,6,7};
int[] b = new int[a.length + 1];
public void insert(int[] arr,int index,int value){
//要插入的位置前面的元素不变
for (int i = 0;i < index - 1 ;i++ ){
b[i] = a[i];
}
//要插入的位置后面的元素整体往后移一个位置
for (int j = a.length;j >= index ;j-- ){
b[j] = a[j - 1];
}
//插入元素
b[index - 1] = value;}
}