应该是一个简单的错误,但是我没有找出来
本帖最后由 爱学习520 于 2022-3-30 20:50 编辑报错是这样的,图片在最后
代码
package cn.nw.algorithm.linear;
import cn.nw.algorithm.linear.SequenceList;
public class Test_linear {
public static void main(String[] args) {
SequenceList<int> l=new SequenceList<>();
l.InSert(1,4);
l.InSert(2,5);
l.InSert(3,6);
l.InSert(4,7);
l.InSert(5,8);
}
}
package cn.nw.algorithm.linear;
public class SequenceList<T> {
//成员变量
private T[] eles;
private int length;
//无参构造方法,初始化默认大小为10
public SequenceList(){
this.eles=(T[]) new Object;
this.length=0;
}
//无参构造方法
public SequenceList(int capacity){
//判断输入的capacity是否合法
if(capacity<=0){
System.out.print("error");
System.exit(1);
}
this.eles=(T[]) new Object;
this.length=0;
}
//在第i处插入一个元素
public boolean InSert(int pos,T obj){
//判断插入位置是否合法
if(pos<1||pos>this.length+1){
System.out.print("插入位置不合法");
return false;
}
//如果插入时容量已满,扩容到当前长度的2倍
if(this.length==eles.length){
//定义一个临时数组temp保存原数组eles
T[] temp=eles;
//将现有的容量扩充为原来的2倍
this.eles=(T[]) new Object;
//将原有的数据依次拷贝到新数组
for(int i=0;i<this.length;i++)
this.eles=temp;
}
//插入操作,所有的数据向下移动,直到腾出一个位置
for(int i=this.length;i>=pos;i--)
this.eles=this.eles;
//插入对应的元素
this.eles=obj;
//该数组长度加一
this.length++;
return true;
}
}
SequenceList<int> = SequenceList<int>
不存在
SequenceList<int> = SequenceList<> ba21 发表于 2022-3-30 20:52
SequenceList = SequenceList
不存在
SequenceList = SequenceList
试过了 没有用呀 加油 加油 {:10_256:} {:10_256:} 666666666666666 顶一顶 {:10_279:} {:5_109:} 需要的是引用数据类型,而int 是基本数据类型 集合不能存放基本数据类,只能存放对象的引用,懂吧{:9_238:} 以为是python
原来是Java {:9_227:} 6666666666 心驰神往 发表于 2022-3-31 08:04
顶一顶
{:10_254:} int改成Integer {:5_109:} {:5_109:}
页:
[1]
2