简体   繁体   English

为什么我在ArrayList中而不是整数值中得到此结果?

[英]Why am I getting this results in my ArrayList instead of the integer values?

Hello and Thanks for all your peoples help. 您好,谢谢大家的帮助。

I am making a concentration game as a learning project. 我正在做一个专注于游戏的学习项目。 I was having some problems with the random generator working right which was solved by Jeeter in this thread. 我在使用随机生成器时遇到了一些问题,该问题已由Jeeter解决。 Jeeter's Solution 吉特解决方案

and this is the code which I am using to get the numbers and store them into an ArrayList 这是我用来获取数字并将其存储到ArrayList中的代码

ArrayList<int[]> al = new ArrayList<int[]>();

int offsetX = 130;
int offsetY = 100;

switch(Game.difficulity){
case EASY:
    GameConstants.cardsToDraw = 6;
    //generate cards
    UniqueRandoms randpEA = new UniqueRandoms(GameConstants.cardsToDraw);
    int[] cardsEA = new int[GameConstants.cardsToDraw];
    System.out.print("The Picks are: "); //Testing purposes only.

    for (int i = 0; i < GameConstants.cardsToDraw; i++) {
       cardsEA[i] = randpEA.nextInt();  // grabs the results so they can be manulipated
       al.add(cardsEA);                 // add the results to an ArrayList
       System.out.print(cardsEA[i] + ", "); //testing to see what is chosen

       // this line just shows the result as cards onscreen.        
       handler.addcard(new GameCard((offsetX) + i*90, (GameConstants.CENTER_Y - offsetY), cardsEA[i], res));
                        }
      //end of generate cards
      System.out.print("\n");
      System.out.println("Contents of al: " + al);
      Game.state = GameState.EASY_GAME;
      break;

This is the easy code block to keep this post short. 这是使本文简短的简单代码块。

the results are this when I run the program: 运行程序时的结果是这样的:
Cards to draw: 6 抽奖卡:6
The Picks are: 5, 2, 4, 1, 3, 6, 选秀权是:5,2,4,1,3,6,
Contents of al: [[I@44d74990, [I@44d74990, [I@44d74990, [I@44d74990, [I@44d74990, [I@44d74990] al的内容:[[I @ 44d74990,[I @ 44d74990,[I @ 44d74990,[I @ 44d74990,[I @ 44d74990,[I @ 44d74990]

What my question is why am I getting the crazy numbers instead of the actual integers and how can I fix this? 我的问题是,为什么我会得到疯狂的数字而不是实际的整数,我该如何解决?

Because I have to do the following: 因为我必须执行以下操作:

  1. Store the results 存储结果
  2. Duplicate the results 复制结果
  3. Shuffle the ArrayList 随机排列ArrayList
  4. retrieve the values when I need to use them 当我需要使用它们时检索它们

So far I am trying to store the results and it does not look like it doing what I expect and then I have to figure out how to duplicate the results in the ArrayList. 到目前为止,我正在尝试存储结果,并且看起来不像我期望的那样,然后必须弄清楚如何在ArrayList中复制结果。

My GitHub Project: Here 我的GitHub项目: 在这里

You need to manually loop through the ArrayList and then through each array in the list to print each value. 您需要手动遍历ArrayList,然后遍历列表中的每个数组以打印每个值。 What you need to do is: 您需要做的是:

 System.out.print("Contents of al: ");
 for(int[] ia: al){ 
    for(int i: ia)
       System.out.print(i + ", ");
 }
 System.out.println();

What it doing now is printing the reference of each array in the list. 现在要做的是打印列表中每个数组的引用。

try printing al.toString() instead of al 尝试打印al.toString()而不是al

al.toString() will format its contents to a readable form. al.toString()会将其内容格式化为可读形式。

EDIT: but if al contains int[] follow the answer posted by Thomas 编辑:但是如果al包含int[]遵循由Thomas发布的答案

Facts: 1) ArrayList cannot store primitives, hence your integers are stored as Integer wrappers 事实:1)ArrayList无法存储基元,因此您的整数存储为Integer包装器

2) the crazy numbers are memory references of the respective Integer objects, and for some reason the toString() implementation of that object return the memory locations 2)疯狂数字是各个Integer对象的内存引用,由于某种原因,该对象的toString()实现返回内存位置

hence you have to loop the ArrayList and use get() to get the values 因此,您必须循环ArrayList并使用get()获取值

edit: 编辑:

just noticed 刚注意到

cardsEA[i] = randpEA.nextInt();  // grabs the results so they can be manulipated
  al.add(cardsEA);

you need to change it as 您需要将其更改为

cardsEA[i] = randpEA.nextInt();  // grabs the results so they can be manulipated
  al.add(cardsEA[i]);

to get the desired result 得到期望的结果

By this little snippet: 通过这个小片段:

for (int i = 0; i < GameConstants.cardsToDraw; i++) {
   cardsEA[i] = randpEA.nextInt();
   al.add(cardsEA);

It seems like what you are wanting to do is not have an ArrayList<int[]> because what you have now is an ArrayList of arrays. 似乎您想要做的是没有ArrayList<int[]>因为您现在拥有的是数组的ArrayList。 By doing al.add(cardsEA); 通过做al.add(cardsEA); on each iteration you are just adding the same array to the list repeatedly. 在每次迭代中,您只是将相同的数组重复添加到列表中。 When you try to print the list you are getting the memory address of the array for each time you added it in the loop. 当您尝试打印列表时,每次将其添加到循环中时,都将获取该数组的内存地址。 It seems like this what you are wanting: 似乎您想要的是这样的:

ArrayList<Integer> al = new ArrayList<Integer>(0);

Then change the line in the loop to this: 然后将循环中的行更改为此:

al.add(cardsEA[i]);

Though if this is the case you don't need to create the array at all. 尽管是这种情况,您根本不需要创建数组。 You can change your loop to this: 您可以将循环更改为此:

for (int i = 0, card; i < GameConstants.cardsToDraw; i++) {
   card = randpEA.nextInt();
   al.add(card);
   System.out.print(card + ", ");

   handler.addcard(new GameCard((offsetX) + i*90, (GameConstants.CENTER_Y - offsetY), card, res));

Alternatively if you want both the array and the list something you can do is this: 或者,如果您同时需要数组和列表,则可以执行以下操作:

Integer[] cardsEA = new Integer[GameConstants.cardsToDraw];

Then don't bother working with the list until after the loop and do either this: 然后,直到循环之后才使用列表,然后执行以下操作:

// declaration
List<Integer> al;

// after the loop
al = Arrays.asList(cardsEA);

Or if you need ArrayList reference instead of List do this: 或者,如果您需要ArrayList引用而不是List,请执行以下操作:

// declaration
ArrayList<Integer> al;

// after the loop
al = new ArrayList<Integer>(Arrays.asList(cardsEA));

Or: 要么:

// declaration
ArrayList<Integer> al = new ArrayList<Integer>();

// after the loop
al.addAll(Arrays.asList(cardsEA));

Lots of ways to make a list from an array. 有很多方法可以从数组中创建列表。 If you want to create the list as starting as empty, if possible instantiate it as new ArrayList<Integer>(GameConstants.cardsToDraw) because then the ArrayList will potentially not need to be internally resized. 如果要创建以空开头的列表,请尽可能将其实例化为new ArrayList<Integer>(GameConstants.cardsToDraw)因为这样一来,可能不需要在内部调整ArrayList的大小。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么当我打印 ArrayList 时得到列表值而不是参考值 - why am I getting List values instead of reference value, when i print ArrayList 为什么我在使用最大 integer 值时得到荒谬的值? - Why I am getting absurd values on using maximum integer value? 为什么我在解决幂集/子集问题时得到空的 2d ArrayList,例如 [ [ ]、[ ]、[ ]、...],而不是在 2d ArrayList 中添加 ArrayList? - Why I am getting empty 2d ArrayList such as [ [ ], [ ], [ ], ... ] instead of adding ArrayList in 2d ArrayList while solving power set/subset problem? 为什么我从正在读取的文件中将更多行添加到阵列列表中? - Why am I getting more lines added to my arraylist from a file I am reading? 为什么我的scaleImage方法会得到奇怪的结果? - Why am I getting a weird results from my scaleImage method? 为什么我从我的算法实现中得到错误的结果? - Why am I getting the wrong results from my algorithm implementations? 为什么我在使用 ArrayList 时出现 Time Limit Exceeded (TLE) 错误<string[]>而不是 int[][]?</string[]> - Why am I getting Time Limit Exceeded (TLE) error when using ArrayList<String[]> instead of int[][]? 如果我从另一个类调用它,为什么我的ArrayList为空? - Why Am I getting my ArrayList empty if I call it from another class? 为什么我无法分配嵌入式ArrayList <Integer> 到本地声明的ArrayList <Integer> ? - Why am I unable to assign an embedded ArrayList<Integer> to a locally declared ArrayList<Integer>? 为什么我的 ArrayList<integer> 包含 null 值?</integer> - Why does my ArrayList<Integer> contain null values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM