[英]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: 因为我必须执行以下操作:
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中复制结果。
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.