62,616
社区成员
发帖
与我相关
我的任务
分享
public class Insert<E> {
protected E value;
protected int position;
protected Object[] arr;
public Insert (E value, int position, Object[] arr){
this.value=value;
this.position=position;
this.arr=arr;
if(position<0 ||position>=arr.length)
throw new IndexOutOfBoundsException();
else if(position==arr.length-1)
this.insert();
else{
this.resize();
this.replace();
this.insert();
}
}
private void insert()
{
arr[position]=value;
}
private void resize()//resize the given array with a new length.
{
int newLength=arr.length+1;
Object[] temp=new Object[newLength];
System.arraycopy(temp, 0, arr, 0, arr.length);
arr=temp;
}
private void replace()
{
for(int i=arr.length-2;i>position;i--)
{
arr[i+1]=arr[i];
}
}
public class MainPanel {
public static void main(String[] args)
{
char[] arr1={'a','b','c','d','f','g','h','i','j'};
Insert<Character> ins=new Insert<Character>('Z',2,arr1);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Object[] arr1={'a','b','c','d','f','g','h','i','j'};
Insert<Character> ins=new Insert<Character>('Z',2,arr1);
}