爱学习520 发表于 2022-3-30 20:44:34

应该是一个简单的错误,但是我没有找出来

本帖最后由 爱学习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;
    }
}


ba21 发表于 2022-3-30 20:52:08

SequenceList<int> = SequenceList<int>
不存在
SequenceList<int> = SequenceList<>

爱学习520 发表于 2022-3-30 20:57:27

ba21 发表于 2022-3-30 20:52
SequenceList = SequenceList
不存在
SequenceList = SequenceList

试过了 没有用呀

tjweiyanmin 发表于 2022-3-30 22:21:58

加油

1molHF 发表于 2022-3-30 22:34:14

加油

1molHF 发表于 2022-3-30 22:35:02

{:10_256:}

1molHF 发表于 2022-3-30 22:46:46

{:10_256:}

amazed 发表于 2022-3-31 00:33:53

666666666666666

心驰神往 发表于 2022-3-31 08:04:02

顶一顶

伽羅~ 发表于 2022-3-31 10:03:50

{:10_279:}

1050293757 发表于 2022-3-31 12:51:59

{:5_109:}

小伤口 发表于 2022-3-31 17:12:45

需要的是引用数据类型,而int 是基本数据类型

小伤口 发表于 2022-3-31 17:16:22

集合不能存放基本数据类,只能存放对象的引用,懂吧{:9_238:}

__add__ 发表于 2022-3-31 19:29:58

以为是python
原来是Java

吴小姗 发表于 2022-3-31 20:30:58

{:9_227:}

amazed 发表于 2022-4-1 00:54:28

6666666666

心驰神往 发表于 2022-4-1 13:49:25

心驰神往 发表于 2022-3-31 08:04
顶一顶

{:10_254:}

lff 发表于 2022-4-1 19:31:57

int改成Integer

1050293757 发表于 2022-4-1 22:21:23

{:5_109:}

1050293757 发表于 2022-4-2 15:41:22

{:5_109:}
页: [1] 2
查看完整版本: 应该是一个简单的错误,但是我没有找出来