gpt4 book ai didi

java - 我的 Vector 丢失数据有什么原因吗?

转载 作者:行者123 更新时间:2023-12-01 17:51:01 26 4
gpt4 key购买 nike

我当前的目标是将对象添加到 vector 中,以便由我创建的另一个类中的迭代器访问(对于这个项目,我不允许使用 Java 的迭代器)。我的迭代器函数应该返回 vector 内的特定对象。相反,迭代器报告 vector 内的空值。调试后,问题是在启动时添加对象后,对象消失了。将 vector 传递到迭代器类并不能解决问题。我尝试过使用数组列表而不是 vector ,但没有成功。

public void init() { //where im doing the adding
for (int i = 0; i < 1; i++) {
cyborg = new Player(ColorUtil.rgb(42, 194, 225), 50, 46.0, baseLocations[0], 40, 100, 100, 0, 50, true);
go.add((Player) cyborg);
}

for (int i = 0; i < 3; i++) {
NPC = new NPC(ColorUtil.rgb(42, 194, 225), 50, 46.0, baseLocations[0], 40, 100, 100, 0, 50, currStrat);
go.add((NPC) NPC);
}

for (int i = 0; i < 2; i++) {
drone = new Drone(ColorUtil.rgb(82, 95, 81), r.nextInt(50), 10.0,
new Point(r.nextFloat() * 1000, r.nextFloat() * 1000), r.nextInt(50));
go.add(drone);
}

for (int i = 0; i < 4; i++) {
base = new Base(ColorUtil.rgb(169, 235, 0), baseSequence++, baseLocations[i], 10);
go.add((Fixed) base);
}

for (int i = 0; i < 2; i++) {
eStation = new eStation(ColorUtil.rgb(100, 85, 85), new Point(r.nextFloat() * 1000, r.nextFloat() * 1000),
r.nextInt(50), 100);
go.add((Fixed) eStation);

}



public class GameCollection implements ICollection {

private Vector<GameObject> gameCollection;

public GameCollection() {
gameCollection = new Vector<GameObject>(); //the vector im having problems with
System.out.println(gameCollection.toString()); //Test to check if objects in game collection array. Prints null values after startup
}

public IIterator getIterator() {

GameCollectionIterator gameItr = new GameCollectionIterator(gameCollection);
return gameItr;


}

public void add(GameObject o) {
gameCollection.addElement(o);
//System.out.println(super.toString());
}



public Object elementAt(int location) {
if(location < gameCollection.size()) {
return (Object) gameCollection.indexOf(location);
}

throw new ArrayIndexOutOfBoundsException(location);

}
public void remove(GameObject o) {
// TODO Auto-generated method stub
gameCollection.remove(gameCollection.indexOf(o));
}




private class GameCollectionIterator implements IIterator{
private int currIndex = 0;

private Vector <GameObject> game = new Vector <GameObject>();

public GameCollectionIterator(Vector<GameObject> g) {
game = g;
}


@Override
public boolean hasNext() {
if(game.size() <= 0){
System.out.println("First case");
return false;

}
if(currIndex == game.size() -1){
System.out.println("Second case");
return false;
}
return true;
}

@Override
public Object getNext() {
currIndex++;
return(game.indexOf(currIndex));
}





@Override
public void remove() {
game.remove(currIndex);
}

}

最佳答案

我可以在这里看到你的迭代器的一些问题:

@Override
public Object getNext() {
currIndex++; // (1)
return(game.indexOf(currIndex)); // (2)
}
  1. currIndex++; - 在从 vector 中获取元素之前增加索引,因此基本上总是跳过第一个元素(索引为 0)。

  2. game.indexOf(currIndex) - indexOf 返回指定元素第一次出现的索引,请参阅 doc 。您必须使用 get 方法,请参阅 doc .

关于java - 我的 Vector 丢失数据有什么原因吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60794892/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com