【问题标题】:Implementing a collection in JAVA with increasing order of elements - "Out of bounds" error在 JAVA 中实现元素顺序递增的集合 - “超出范围”错误
【发布时间】:2018-04-04 14:02:39
【问题描述】:

我的任务是实现一个按升序排列的元素集合,以及向集合中添加元素、打印集合中的所有元素和加载元素的方法(以及从集合中删除它,我可以假设我总是加载最小的)。 我应该使用Comparable<T> 接口。

另外,我需要使用Comparable<T>接口实现一个类层次结构(例如,它可以是一个军衔层次结构)。

这是我的代码:

public class Collection<T extends Comparable<T>> implements Iterable<T>
{
    public LinkedList<T> collection;
    public Collection()
    {
        collection = new LinkedList<>();
    }

    public void addToList(T new)
    {
        int i = 0;
        while (collection .get(i).compareTo(new) < 0)
        {
            i++;
        }
        collection.add(i, new);
    }

    public T load() throws EmptyStackException
    {
        if (collection.size() == 0)
        {
            throw new EmptyStackException();
        }
        T first = collection.getFirst(); 
        collection.removeFirst();
        return first;
    }

    public void printElements()
    {
        for (T obj : collection)
        {
            System.out.println(obj);
        }
    }

@Override
public Iterator<T> iterator()
{
    return this.collection.iterator();
}

}

public abstract class Soldier implements Comparable<Soldier>
{
    public String Name;

    public abstract double Rank();

    public int compareTo(Soldier S)
    {
        if(S.Rank() == this.Rank())
        {
            return 0;
        }
        else if (S.Rank() < this.Rank())
        {
            return 1;
        }
        else return -1;
    }
}

public class General extends Soldier
{
    public double Rank()
    {
        return 4;
    }
    public General(String Name)
    {
        this.Name = Name;
    }
}

public class Colonel extends Soldier
{
    public double Rank()
    {
        return 3;
    }
    public Colonel(String Name)
    {
        this.Name = Name;
    }
}

public class Corporal extends Soldier
{
    public double Rank()
    {
        return 2;
    }
    public Corporal(String Name)
    {
        this.Name = Name;
    }
}

public class Private extends Soldier
{
    public double Ranga()
    {
        return 1;
    }
    public Private(String Name)
    {
        this.Name = Name;
    }
}

当我尝试运行一些测试时,我收到一个错误“索引超出范围”。这里实际发生了什么?我怀疑我无法正确地将元素添加到我的收藏中。这段代码正确吗?

【问题讨论】:

  • 已经有一个名为Collection的接口。我强烈建议您将您的更改为,例如,MyCollection,以避免名称冲突和导致不可避免的头痛。
  • kolekcja 中的 addToList 定义在哪里?

标签: java collections interface comparable


【解决方案1】:

问题可以在这里隔离:

public LinkedList<T> collection;

public Collection()
{
    collection = new LinkedList<>();
}

public void addToList(T new)
{
    int i = 0;
    while (collection.get(i).compareTo(new) < 0)
    {
        i++;
    }
    collection.add(i, new);
}

第一次尝试添加元素时,将零传递给collection.get。这会尝试获取第一个元素(因为 Lists 是零索引),但没有要获取的第一个元素。


另外,'new' 是 Java 中的关键字,不能用作标识符。

【讨论】:

    【解决方案2】:

    你的问题在于这个循环

    while (collection .get(i).compareTo(new) < 0)
    {
        i++;
    }
    

    即使i 等于或大于集合的长度,它也会尝试获取新元素。你需要检查它不是。

    while (i < collection.size() && collection .get(i).compareTo(new) < 0)
    {
        i++;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多