Java 为什么我在ArrayList中得到这个结果而不是整数值?
大家好,谢谢大家的帮助 我正在做一个专注游戏作为一个学习项目。我遇到了一些随机发生器正常工作的问题,Jeeter在这个线程中解决了这些问题 这是我用来获取数字并将其存储到ArrayList中的代码Java 为什么我在ArrayList中得到这个结果而不是整数值?,java,arraylist,Java,Arraylist,大家好,谢谢大家的帮助 我正在做一个专注游戏作为一个学习项目。我遇到了一些随机发生器正常工作的问题,Jeeter在这个线程中解决了这些问题 这是我用来获取数字并将其存储到ArrayList中的代码 ArrayList<int[]> al = new ArrayList<int[]>(); int offsetX = 130; int offsetY = 100; switch(Game.difficulity){ case EASY: GameConstant
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;
ArrayList al=new ArrayList();
int offsetX=130;
int offsetY=100;
切换(游戏难度){
简单案例:
gamesconstants.cardsToDraw=6;
//生成卡片
UniqueRandoms randpEA=新的UniqueRandoms(GameConstants.cardsToDraw);
int[]cardsEA=新int[gamesconstants.cardsToDraw];
System.out.print(“选择是:”;//仅用于测试目的。
对于(int i=0;i
这是保持这篇文章简短的简单代码块
运行程序时的结果如下:抽牌:6张
选择是:5、2、4、1、3、6、
al的内容:[[I@44d74990, [I@44d74990, [I@44d74990, [I@44d74990, [I@44d74990, [I@44d74990]
我的问题是,为什么我得到的是疯狂的数字而不是实际的整数,我该如何解决这个问题 因为我必须做以下几件事:
My GitHub项目:您需要手动循环遍历ArrayList,然后遍历列表中的每个数组以打印每个值。您需要做的是:
System.out.print("Contents of al: ");
for(int[] ia: al){
for(int i: ia)
System.out.print(i + ", ");
}
System.out.println();
它现在正在打印列表中每个数组的引用。尝试打印
al.toString()
而不是al
al.toString()
将其内容格式化为可读形式
编辑:但如果al
包含int[]
则按照托马斯发布的答案进行操作事实:
1) ArrayList不能存储原语,因此整数被存储为整数包装器
2) 疯狂数字是各个整数对象的内存引用,出于某种原因,该对象的toString()实现返回内存位置
因此,必须循环ArrayList并使用get()获取值
编辑:
刚刚注意到
cardsEA[i] = randpEA.nextInt(); // grabs the results so they can be manulipated
al.add(cardsEA);
您需要将其更改为
cardsEA[i] = randpEA.nextInt(); // grabs the results so they can be manulipated
al.add(cardsEA[i]);
要通过这个小片段获得所需的结果:
for (int i = 0; i < GameConstants.cardsToDraw; i++) {
cardsEA[i] = randpEA.nextInt();
al.add(cardsEA);
然后将循环中的行更改为:
al.add(cardsEA[i]);
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));
如果是这种情况,则根本不需要创建数组。您可以将循环更改为:
al.add(cardsEA[i]);
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));
然后,在循环结束之前,不要费心处理列表,执行以下操作之一:
// declaration
List<Integer> al;
// after the loop
al = Arrays.asList(cardsEA);
//声明
列表al;
//循环后
al=数组.asList(cardsEA);
或者,如果需要ArrayList引用而不是List,请执行以下操作:
// declaration
ArrayList<Integer> al;
// after the loop
al = new ArrayList<Integer>(Arrays.asList(cardsEA));
//声明
ArrayList al;
//循环后
al=新的ArrayList(Arrays.asList(cardsEA));
或:
//声明
ArrayList al=新的ArrayList();
//循环后
al.addAll(Arrays.asList(cardsEA));
从数组创建列表的方法很多。如果您想创建一个以空开头的列表,如果可能的话,将其实例化为
newArrayList(GameConstants.cardsToDraw)
因为这样,ArrayList可能不需要在内部调整大小。al的类型是什么??我只想说Jeeter的解决方案非常糟糕…@ZongZhengLi这与正确存储数组中的值有什么关系?没什么,所以如果你认为这样做不好,为什么要发布这样无用的注释呢我可以做得更好,然后展示一个例子,解释为什么其他人可以从中学习。对不起,我不是有意冒犯你或嘲笑你。只是实现和算法都很糟糕,特别是对于初学者来说。你已经发布了它,所以我可以对它进行评论,尽管我承认这不是最具建设性的评论。我稍后可能会提供一个更好的例子。@smcg al是这样的,我在发布此问题之前忘记添加它,我返回并添加了它,但您已经发表了评论。ArrayList al=new ArrayList();我不必打印出数组的内容打印出来是为了我自己的调试我需要做的是确保我的数组列表中有整数而不是它显示的那些疯狂的垃圾。然后我需要复制存储在数组列表中的数字。这样做。它会给你你想要的。“疯狂的垃圾”是列表中每个in[]的内存地址。如果要查看内容,必须访问每个数组的每个元素。通过声明ArrayList
实际上可以非常确定它只包含int[]
s。即使您尝试在其中添加其他内容,忽略警告并进行编译,您将在运行时遇到不可编译的源代码异常,程序将崩溃。我尝试过这样做,但我遇到了一些其他错误,我不知道如何修复。它希望我更改代码中的其他内容,当我告诉它这样做时,我仍然有错误我不知道如何修复。我将更改它,获取错误并将其发布到此处。您可以发布错误吗?我认为问题出现在您定义ArrayList时,您将其定义为在int[]上有一个数组,因此
// declaration
ArrayList<Integer> al = new ArrayList<Integer>();
// after the loop
al.addAll(Arrays.asList(cardsEA));