Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ArrayList索引在for循环中落后?_Java_For Loop_Arraylist - Fatal编程技术网

Java ArrayList索引在for循环中落后?

Java ArrayList索引在for循环中落后?,java,for-loop,arraylist,Java,For Loop,Arraylist,所以我是爪哇的一名助教,刚开始在大学学习语言 在尝试将链接到对象的变量值与插入的值进行比较以确保没有重复值时,我遇到了一个问题。由于缺乏更好的表达,对象似乎“落后”。有关更好的解释,请参见代码注释 只有当你添加两个或更多独立的武器,然后尝试重新添加一个同名武器时,问题才会出现,它允许你这样做。如果你只添加了一件武器,然后尝试重新添加,它会说没有,代码会按预期工作 所以,是的,当我调用printHeroInfo方法时,我仍然看到我可以得到几个同名的武器。还有另一个类叫做超级武器,如果你认为里面的代

所以我是爪哇的一名助教,刚开始在大学学习语言

在尝试将链接到对象的变量值与插入的值进行比较以确保没有重复值时,我遇到了一个问题。由于缺乏更好的表达,对象似乎“落后”。有关更好的解释,请参见代码注释

只有当你添加两个或更多独立的武器,然后尝试重新添加一个同名武器时,问题才会出现,它允许你这样做。如果你只添加了一件武器,然后尝试重新添加,它会说没有,代码会按预期工作

所以,是的,当我调用printHeroInfo方法时,我仍然看到我可以得到几个同名的武器。还有另一个类叫做超级武器,如果你认为里面的代码与这个问题有关,那么请告诉我,我会在一封信或其他东西中发布

提前谢谢你帮我搔头

import java.util.ArrayList;
public class superHero{
private ArrayList<superWeapon> superWeaponList;
private String superHeroName;
private String superHeroInfo;

public superHero(String superHeroName, String superHeroInfo){
    this.superHeroName=superHeroName;
    this.superHeroInfo=superHeroInfo;
    superWeaponList=new ArrayList<superWeapon>();
}
public void addSuperWeapon(String superWeaponName, int superWeaponCharges){
    superWeapon Obj = new superWeapon(superWeaponName, superWeaponCharges); // Making an object of the superWeapon class.
    int size=superWeaponList.size(); // Gets the size of the array superWeaponList.
    if(size==0){ /* If the size of the arary is zero, that means there are no weapons in it currently.
        For that reason we don't need the for-loop we otherwise would need to compare the name of the weapon
        we are trying to add against the pre-existing names of weapons in the array. */
        superWeaponList.add(Obj);
        System.out.println("The Superweapon"+superWeaponName+" has been added for the superhero "+superHeroName+".");
        System.out.println(superWeaponName+" has "+superWeaponCharges+" charges.");
    }
    else{ /* If the size of the array is NOT zero, we need to compare the name of the weapon we are trying to add
        against the weapons already existing in the array. This we do with the following for loop. */
        for(int i=0;i<size;i++){
            superWeapon temp_obj=superWeaponList.get(i);
            String temp_name = temp_obj.getName();
            System.out.println(temp_name); /* Why does the name lag behind one entry?
On the second entry it also shows the first entry. On the third entry it shows the second
entry and so on... The first place in an arraylist should be index=0,
so the for-loop starting at 0 should also be correct, right?
I just added this print to see what the current value for getName()
would end up returning. This is how I found the bug/error. */
            if(temp_name!=superWeaponName){ // If the name doens't match then we add a new superweapon.
                superWeaponList.add(Obj);
                System.out.println("The superweapon "+superWeaponName+" has been added for the superhero "+superHeroName+".");
                System.out.println(superWeaponName+" has "+superWeaponCharges+" charges.");
            }else{ // If the names match however, we don't add the new weapon we were trying to add.
                System.out.println("There already is a superweapon with the name "+superWeaponName+
                " registered for the superhero "+superHeroName);
            }

        }
    }
}
public void printHeroInfo(){
    System.out.println(superHeroName);
    System.out.println(superHeroInfo);
    int size=superWeaponList.size();
    for(int i=0;i<size;i++){
        superWeapon temp_obj=superWeaponList.get(i);
        System.out.println(superHeroName+" has the superweapon "+temp_obj.getName()+" with "+temp_obj.getCharges()+" charges.");
    }
}
import java.util.ArrayList;
公众级超级英雄{
私人阵列列表超级武器列表;
私有字符串超级英雄名;
私有字符串超级英雄信息;
公共超级英雄(字符串超级英雄名、字符串超级英雄信息){
this.superHeroName=superHeroName;
this.superHeroInfo=superHeroInfo;
超级武器列表=新阵列列表();
}
public void addsuperweaponname(字符串superWeaponName,int superWeaponCharges){
超级武器Obj=新的超级武器(超级武器名称,超级武器电荷);//生成超级武器类的对象。
int size=superWeaponList.size();//获取数组superWeaponList的大小。
如果(大小==0){/*如果阿拉伯半岛的大小为零,这意味着它目前没有武器。
因此,我们不需要For循环,否则需要比较武器的名称
我们正试图对阵列中已有的武器名称进行添加*/
超级武器清单(Obj);
System.out.println(“超级武器”+超级武器名+”已为超级英雄“+超级英雄名+”添加);
System.out.println(超级武器名称+“有”+超级武器电荷+“电荷”);
}
否则{/*如果数组的大小不是零,我们需要比较我们试图添加的武器的名称
针对阵列中已经存在的武器。我们使用以下for循环进行此操作*/

对于(inti=0;i,虽然使用Set而不是list可以改进很多,但我将指出您在比较字符串时所做的假设中的错误

 if(temp_name!=superWeaponName)  //incorret
 if(!temp_name.equals(superWeaponName))  //correct

有这么多注释的代码真的很难读懂。请尽可能避免您的问题和代码相互暴露使用专用集superWeaponList;superWeaponList=new ArrayList();这样会更理智、更快。。。