向各位路过的大神求问

seybard 2012-10-13 11:04:50
我自己在写这段程序的时候IDE报错,但不知道自己怎么改,求大神帮助

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

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不久,很多都还不懂,问题虽愚蠢,但求路过的大神赐教。
...全文
175 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
杀手 2012-10-13
  • 打赏
  • 举报
回复
Object换成char
seybard 2012-10-13
  • 打赏
  • 举报
回复
补充下,我要干的事情是把一个element放进一个已经有的数组里且不能把原数组里的数据顶掉。
seybard 2012-10-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
测试了下:
应该把数组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'};……
[/Quote]
谢谢!
幽y 2012-10-13
  • 打赏
  • 举报
回复
测试了下:
应该把数组char[] arr1 设置为Object类型。


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);

}


thank you in advance for giving me credits.
seybard 2012-10-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

Object换成char
[/Quote]
多谢大哥回复。是我没说清楚,那个insert class想做成通用的。就是我main测试的时候,用任何类型的array都能跑(String, int , char, etc.)

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧