【问题标题】:Why does a integer, after put into an array, change values?为什么整数放入数组后会改变值?
【发布时间】:2012-04-16 22:40:14
【问题描述】:

解决了

我编写了一个程序,它在等号之后加载字符串,并让它计算它执行了多少次。数完后,我告诉它告诉我 int 有多大。我正在寻找的值是 3,它告诉我,3。然后我将其更改为字符串,该值保持为 3。然后,我将它放入一个 4d 数组中,它告诉我值为 2。发生了什么?

代码:

                   int times=0;
                   else if (list.equals("Weapon")) {//If the word weapon is before the = 
                        weapon = value; //take the string after the = and put it into String weapon
                        troopStats[times][1][weaponTimes][0] = weapon;
                        weaponTimes++;
                        System.out.println(weaponTimes+"weapontimes"+times);
                    }

                        weaponTimesStr = Integer.toString(weaponTimes);
                        System.out.println(weaponTimesStr+"string");
                        troopStats[times][1][0][1] = weaponTimesStr;
                        System.out.println(troopStats[times][1][0][1]+"InArray");
                        times++
                        //loops

输出:

3weapontimes    //Counted the equals sign 3 times, Note that this is from the part of the 
                 omitted code
3string         // Changed the integer to a string and got 3
2InArray        // Put it into an array, and got 2 back

发生了什么事?

(我知道我可以将值加 1,但我想稍后将此代码用于未知数量的事情)

为了帮助,我已经发布了整个代码:

public class TroopLoader {
    static String[][][][] troopStats;
    static int times = 0;
    static int weaponTimes = 0;
    static int armorTimes = 0;
    static int animalTimes = 0;
    static String weaponTimesStr;
    static String armorTimesStr;
    static String animalTimesStr;
    static String troop;
    static String weapon;
    static String armor;
    static String animal;
    static String speed;
    static int total = 0;

    /*
     * [][][]
     * 
     * [total number of troops (total)]
     * 
     * [stats] 0= name 1= weapon 2= armor 3= animal 4= speed
     * 
     * [different things within stat]
     */

    public void readTroop() {

        File file = new File("resources/objects/troops.txt");
        BufferedReader reader = null;

        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            // repeat until all lines is read
            while ((text = reader.readLine()) != null) {
                StringTokenizer troops = new StringTokenizer(text, "=");
                if (troops.countTokens() == 2) {
                    String list = troops.nextToken();

                    if (list.equals("Troop")) {
                        total++;
                    }

                    else {

                    }
                } else {

                }

            }
            troopStats = new String[total][5][10][2];

        }

        catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        File file2 = new File("resources/objects/troops.txt");
        BufferedReader reader2 = null;

        try {
            reader2 = new BufferedReader(new FileReader(file2));
            String text = null;

            // repeat until all lines is read
            while ((text = reader2.readLine()) != null) {
                StringTokenizer troops = new StringTokenizer(text, "=");
                if (troops.countTokens() == 2) {
                    String list = troops.nextToken();
                    String value = troops.nextToken();

                    if (list.equals("Troop")) {
                        troop = value;

                        troopStats[times][0][0][0] = troop;
                    }

                    else if (list.equals("Weapon")) {
                        weapon = value;
                        troopStats[times][1][weaponTimes][0] = weapon;
                        weaponTimes++;
                        System.out.println(weaponTimes+"weapontimes"+times);
                    }

                    else if (list.equals("Armor")) {

                        armor = value;

                        troopStats[times][2][armorTimes][0] = armor;
                        armorTimes++;
                    }

                    else if (list.equals("Animal")) {

                        animal = value;

                        troopStats[times][3][animalTimes][0] = animal;
                        animalTimes++;
                    }

                    else if (list.equals("Speed")) {

                        speed = value;

                        troopStats[times][4][0][0] = speed;

                    }

                    else if (list.equals("Done")) {
                        weaponTimesStr = Integer.toString(weaponTimes);
                        System.out.println(weaponTimesStr+"string");
                        armorTimesStr = Integer.toString(armorTimes);
                        animalTimesStr = Integer.toString(animalTimes);
                        troopStats[times][1][0][1] = weaponTimesStr;
                        troopStats[times][1][0][1] = armorTimesStr;
                        troopStats[times][1][0][1] = animalTimesStr;
                        System.out.println(troopStats[times][1][0][1]+"InArray"+times);
                        times++;
                        troop = "";
                        weapon = "";
                        armor = "";
                        animal = "";
                        speed = "";
                        weaponTimes = 0;
                        armorTimes = 0;
                        animalTimes = 0;

                    }

                    else {

                    }

                } else {

                }

            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        finally {
            try {
                if (reader2 != null) {
                    reader2.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

在代码的前面部分,我让程序将一个值存储在具有武器时间变量的数组位置,而不是存储武器时间变量。我的错,很抱歉浪费您的时间。

【问题讨论】:

  • 我敢猜测times 不是你想的那样。
  • 你能显示SSCCE吗? weaponTimesStringweaponTimesStr 是一样的吗? times 可以在第 3 行和第 4 行之间更改(由另一个线程访问)吗?
  • 在调试器中单步执行代码时会看到什么。这种变化发生在哪一行?
  • 您分配了weaponTimesString,但您使用了weaponTimesStr。这是问题中的复制粘贴错字吗?还是在代码中? [无论如何我怀疑这是问题所在,因此这只是一个评论 - 不是答案]
  • @Russell 您的 IDE 很可能使您能够插入断点并逐步运行程序,以便您可以检查每行代码的变量值。

标签: java string multidimensional-array int


【解决方案1】:

我用你发布的内容写了一个SSCCE,它打印了你所期望的:

public static void main(String[] args) {
    String[][][][] troopStats = new String[4][4][4][4];
    int times = 2;
    int weaponTimes = 3;
    String weaponTimesStr = Integer.toString(weaponTimes);
    System.out.println(weaponTimesStr + "string"); //prints 3string
    troopStats[times][1][0][1] = weaponTimesStr;
    System.out.println(troopStats[times][1][0][1] + "InArray"); //prints 3InArray
}

所以问题很可能出在其他地方。

【讨论】:

  • 谢谢,我会继续努力的。哦,顺便说一句,在断点处 times=0。
【解决方案2】:

以下内容:

public class Foo {
  public static void main(String[] args) {
    String[][][][] troopStats = new String[2][2][2][2];
    String weaponTimesStr = Integer.toString(3);
    System.out.println(weaponTimesStr+"string");
    troopStats[0][1][0][1] = weaponTimesStr;
    // You said in a comment that 'times' is equal to 0 in this case so have subbed that in
    System.out.println(troopStats[0][1][0][1]+"InArray");
  }
}

给我预期的输出:

3string
3InArray

【讨论】:

    【解决方案3】:

    对不起,我浪费了您的时间,我的错误是因为我使用武器时间的值将值存储在数组中,而不是将武器时间存储在数组中。

    troopStats[times][1][weaponTimes][0] = weapon;
    

    那是错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      • 2021-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多