我自己在写这段程序的时候IDE报错,但不知道自己怎么改,求大神帮助
- Java code
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]; } }
下面是main
- Java code
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); }
其中这段报错Insert<Character> ins=new Insert<Character>('Z',2,arr1);
我不知道问题在哪里。我刚学JAVA不久,很多都还不懂,问题虽愚蠢,但求路过的大神赐教。
------解决方案--------------------------------------------------------
测试了下:
应该把数组char[] arr1 设置为Object类型。
- Java code
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); }