在 JAVA 中实现元素顺序递增的集合 - "Out of bounds" 错误
Implementing a collection in JAVA with increasing order of elements - "Out of bounds" error
我有一个任务是按升序实现元素集合,以及将元素添加到集合的方法,打印集合中的所有元素并加载元素(以及从集合中删除它,我可以假设我总是加载最小的)。
我应该使用 Comparable<T>
界面。
此外,我需要使用 Comparable<T>
接口实现一个 class 层次结构(例如,它可以是军衔层次结构)。
这是我的代码:
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;
}
}
当我尝试 运行 一些测试时,我遇到了错误 "Index out of bounds"。这里到底发生了什么?我怀疑我无法正确地将元素添加到我的集合中。这段代码正确吗?
问题可以在这里隔离:
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
。这会尝试获取第一个元素(因为 List
是 zero-indexed)但是没有要获取的第一个元素。
此外,'new'是Java中的关键字,不能用作标识符。
你的问题出在这个循环
while (collection .get(i).compareTo(new) < 0)
{
i++;
}
即使i
等于或大于集合的长度,它也会尝试获取新元素。你需要检查它不是。
while (i < collection.size() && collection .get(i).compareTo(new) < 0)
{
i++;
}
我有一个任务是按升序实现元素集合,以及将元素添加到集合的方法,打印集合中的所有元素并加载元素(以及从集合中删除它,我可以假设我总是加载最小的)。
我应该使用 Comparable<T>
界面。
此外,我需要使用 Comparable<T>
接口实现一个 class 层次结构(例如,它可以是军衔层次结构)。
这是我的代码:
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;
}
}
当我尝试 运行 一些测试时,我遇到了错误 "Index out of bounds"。这里到底发生了什么?我怀疑我无法正确地将元素添加到我的集合中。这段代码正确吗?
问题可以在这里隔离:
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
。这会尝试获取第一个元素(因为 List
是 zero-indexed)但是没有要获取的第一个元素。
此外,'new'是Java中的关键字,不能用作标识符。
你的问题出在这个循环
while (collection .get(i).compareTo(new) < 0)
{
i++;
}
即使i
等于或大于集合的长度,它也会尝试获取新元素。你需要检查它不是。
while (i < collection.size() && collection .get(i).compareTo(new) < 0)
{
i++;
}