Java 我的向量丢失数据的原因是什么?

Java 我的向量丢失数据的原因是什么?,java,vector,iterator,Java,Vector,Iterator,我现在的目标是将对象添加到向量中,以便由我创建的另一个类中的迭代器访问(对于这个项目,我不允许使用Java的迭代器)。我的迭代器函数应该返回向量中的特定对象。相反,迭代器报告向量中的空值。调试后,问题是在启动时添加对象后,对象将消失。将向量传递到迭代器类并不能解决这个问题。我尝试使用数组列表而不是向量,但运气不好 public void init() { //where im doing the adding for (int i = 0; i < 1; i++) {

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

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);
    }

}
public void init(){//我正在添加
对于(int i=0;i<1;i++){
电子人=新玩家(ColorUtil.rgb(42194225),50,46.0,baseLocations[0],40100100,0,50,true);
加入((玩家)电子人);
}
对于(int i=0;i<3;i++){
NPC=新NPC(ColorUtil.rgb(42194225),50,46.0,基准位置[0],40100100,0,50,currstat);
go.add((NPC)NPC);
}
对于(int i=0;i<2;i++){
无人机=新的无人机(ColorUtil.rgb(82,95,81),r.nextInt(50),10.0,
新点(r.nextFloat()*1000,r.nextFloat()*1000),r.nextInt(50));
加入(无人机);
}
对于(int i=0;i<4;i++){
base=newbase(ColorUtil.rgb(169235,0),baseSequence++,baseLocations[i],10);
go.添加((固定)底座);
}
对于(int i=0;i<2;i++){
eStation=新eStation(ColorUtil.rgb(100,85,85),新点(r.nextFloat()*1000,r.nextFloat()*1000),
r、 耐信(50),100);
go.添加((固定)地产);
}
公共类GameCollection实现ICollection{
私人媒介收集;
公共游戏机收藏(){
gameCollection=new Vector();//我遇到的向量有问题
System.out.println(gameCollection.toString());//测试以检查游戏集合数组中的对象。启动后打印空值
}
公共IIiterator getIterator(){
GameCollectionIterator gameItr=新GameCollectionIterator(gameCollection);
返回gameItr;
}
公共无效添加(游戏对象o){
gameCollection.addElement(o);
//System.out.println(super.toString());
}
公共对象元素(int位置){
如果(位置如果(game.size()我可以在这里看到迭代器存在一些问题:

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

  • game.indexOf(currendex)
    -
    indexOf
    返回指定元素第一次出现的索引,请参见。您必须改用
    get
    方法,请参见


  • 首先,我们无法实际看到发生了什么,因为您没有提供一种“驱动程序”,以便我们了解这些类是如何使用的

    但是,我怀疑问题在于您的
    GameCollectionIterator
    不正确。它有许多问题,包括:

  • next
    方法假设存在下一个元素。它没有检查。(好吧,我们不知道
    IIterator
    合同说应该发生什么,但这是非常可疑的。)

  • 正如@rxn1d还指出的,
    next
    在使用它之前增加
    currendex
    (以零开始)。这意味着迭代器在大多数情况下会跳过第一个元素

  • @rxn1d对于
    返回(game.indexOf(currendex));
    也是正确的。该语句实际上将:

    • 自动箱
      currendex
      整数
    • 尝试在列表中查找
      整数
    • 失败…给出
      int
      -1
      ,以及
    • 自动箱并返回该1
  • 迭代器实现中没有检测到元素在迭代时被添加或删除的情况。当您删除任何对象时,删除点之外的其余对象的位置将发生更改。但是您没有为此调整
    currendex
    。因此,删除可能会导致对象丢失波普

  • 迭代器将不是线程安全的。(不清楚这是否重要…)

  • 请注意,
    java.util.Collection
    的标准(非并发)实现将检测到并发修改,并准确地抛出异常以避免类似4的问题


    还有一些事情需要纠正

    • 在其他人阅读代码之前,您应该先去掉多余的空行和自动生成的注释2

    • 我不明白为什么您要使用自己的
      IIterator
      ICollection
      接口……或者它们的实际含义。(声明在哪里?javadocs在哪里?)

    • 您的
      抛出新的ArrayIndexOutOfBoundsException(location);
      抛出了错误的异常。您的集合不是数组

    • eStation
      是一个错误的类名。类名总是以大写字母开头。然后通过声明一个与其类名称完全相同的变量来复合它

    • 假设
      eStation
      GameObject
      的子类型,则在
      go.add((固定)eStation);
      中强制转换的类型是不必要的。这方面的其他示例