写两个方法,其中一个方法可以把某个整数插入到整型数组的特定位置,数组中原有元素向后移动 另一个方法能够从整形数组中特定位置删除掉一个元素,数组中原有元素向前移动
import java.util.Arrays;
import java.util.List;
public class ArrayDemo1 {
static String[] array={"1","2","3",null,"5"};
static int index=array.length;
public static void main(String[] args) {
add(2, "123");
add(2, "123");
delete(2);
delete(2);
List list=Arrays.asList(array);
System.out.println(list);
}
public static void add(int pos, String value) {
String[] temp;
// if(pos>=index*2){
// temp=new String[pos+1];
// System.arraycopy(array, 0, temp, 0, array.length);
// array=temp;
// index=pos;
// }
if (index > array.length - 1) {
temp = new String[array.length * 3 / 2];
System.arraycopy(array, 0, temp, 0, array.length);
array = temp;
}
for (int i = index; i > pos; i--) {
array[i] = array[i - 1];
}
array[pos] = value;
index++;
}
public static void delete(int pos){
index--;
for(int i=pos;i<index;i++){
array[i]=array[i+1];
}
array[index]=null;
}
}