在Java中的整数数组的ArrayList [英] ArrayList of integer arrays in Java

查看:111
本文介绍了在Java中的整数数组的ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家。

我刚刚进入Java和我试图写一个简单的游戏,一个敌人追逐在网格上的球员。我使用的是简单的算法从维基百科页面上的寻路寻路。这涉及到用含3整数每个列表项创建两个列表。下面是测试code我尝试建立并显示这样的列表。

当我运行下面的code,它打印出相同的号码在ArrayList的每个阵列。它为什么要这样做呢?

 公共类ListTest {公共静态无效的主要(字串[] args){
    ArrayList的<整数[]>名单=新的ArrayList<整数[]>();
    整数[] =点新的整数[3];
    对(INT I = 0; I&小于10;我++){
        为(中间体J = 0; J&下; 3; J ++){
            点[J] =(INT)(的Math.random()* 10);
        }        //这难道不是行添​​加填充整数[]指向
        // ArrayList的名单的结束?
        list.add(点);        //加入这一行,以确认整数[]点实际上是
        //填充有3个随机整数。
        的System.out.println(点[0] +,+点[1] +,+点[2]);
    }
    的System.out.println();    //我现在的理解是,这部分应通过步
    // ArrayList的列表,并检索上面添加的每个整数[]点。它运行,但只
    //最后整数[]点的来自上述的值显示的10倍。
    迭代它= list.iterator();
    而(it.hasNext()){
        点=(整数[])it.next();
        的for(int i = 0;我3;;我++){
            System.out.print(点[I] +,);
        }
            的System.out.println();
        }
    }
}


解决方案

首先,一些其他的答案是误导性的和/或不正确。需要注意的是阵列是一个对象。所以,你可以在列表中使用它们的元素,不管是否数组本身包含基本类型或对象的引用。

接下来,声明一个变量为列表< INT [] GT;列表是pferred在声明为 $ P $的ArrayList< INT []> 。这使您可以轻松地修改列表的LinkedList 或其他一些执行不破坏你的$ C $其余C,因为它是保证使用中的列表接口仅适用方法。欲了解更多信息,你应该研究编程的接口。

现在回答你的真正问题,这是只加注释。让我们来看看你的code几行:

 整数[] =点新的整数[3];

这行创建整数 S,效果显着。

的数组

 的for(int i = 0;我小于10;我++){
    为(中间体J = 0; J&下; 3; J ++){
        点[J] =(INT)(的Math.random()* 10);
    }    //这难道不是行添​​加填充整数[]指向
    // ArrayList的名单的结束?
    list.add(点);
    // ...
}

在这里,您将值分配给数组的元素,然后到数组添加到您的列表 A 参考。每次循环迭代,分配新值同一阵列并添加另一个对同一阵列列表。这意味着列表有10个引用同一阵列已多次写了。

迭代它= list.iterator();
    而(it.hasNext()){
        点=(整数[])it.next();
        的for(int i = 0;我3;;我++){
            System.out.print(点[I] +,);
        }
            的System.out.println();
        }
    }

现在这个循环打印出的同一阵列 10倍。数组中的值设置为previous循环结束的最后一个人。

要解决这个问题,你只需要确保创建10 不同的阵列。

最后一个问题:如果你声明的Iterator<整数[]>它(或的Iterator< INT []>其),你不需要投的返回值,它。接下来()。事实上,这是preferred,因为它是类型安全的。

最后,我要问什么 INT s分别阵列重新present吗?您可能要重新审视你的程序设计和创建包含这三个 INT 个类,无论是作为数组或三个成员变量。

everyone.

I'm just getting into Java, and I'm trying to write a simple game where an enemy chases the player on a grid. I'm using the simple algorithm for pathfinding from the Wikipedia page on pathfinding. This involves creating two lists with each list item containing 3 integers. Here's test code I'm trying out to build and display such a list.

When I run the following code, it prints out the same numbers for each array in the ArrayList. Why does it do this?

public class ListTest {

public static void main(String[] args) {
    ArrayList<Integer[]> list = new ArrayList<Integer[]>(); 
    Integer[] point = new Integer[3];
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 3; j++) {
            point[j] = (int)(Math.random() * 10);
        }            

        //Doesn't this line add filled Integer[] point to the 
        //end of ArrayList list?
        list.add(point);      

        //Added this line to confirm that Integer[] point is actually 
        //being filled with 3 random ints.
        System.out.println(point[0] + "," + point[1] + "," + point[2]);
    }
    System.out.println();

    //My current understanding is that this section should step through 
    //ArrayList list and retrieve each Integer[] point added above. It runs, but only 
    //the values of the last Integer[] point from above are displayed 10 times.
    Iterator it = list.iterator();
    while (it.hasNext()) {
        point = (Integer[])it.next();  
        for (int i = 0; i < 3; i++) {
            System.out.print(point[i] + ",");
        } 
            System.out.println(); 
        } 
    }
}

解决方案

First of all, several of the other answers are misleading and/or incorrect. Note that an array is an object. So you can use them as elements in a list, no matter whether the arrays themselves contain primitive types or object references.

Next, declaring a variable as List<int[]> list is preferred over declaring it as ArrayList<int[]>. This allows you to easily change the List to a LinkedList or some other implementation without breaking the rest of your code because it is guaranteed to use only methods available in the List interface. For more information, you should research "programming to the interface."

Now to answer your real question, which was only added as a comment. Let's look at a few lines of your code:

Integer[] point = new Integer[3];

This line creates an array of Integers, obviously.

for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 3; j++) {
        point[j] = (int)(Math.random() * 10);
    }            

    //Doesn't this line add filled Integer[] point to the 
    //end of ArrayList list?
    list.add(point);
    //...
}

Here you assign values to the elements of the array and then add a reference to the array to your List. Each time the loop iterates, you assign new values to the same array and add another reference to the same array to the List. This means that the List has 10 references to the same array which has been repeatedly written over.

Iterator it = list.iterator(); while (it.hasNext()) { point = (Integer[])it.next(); for (int i = 0; i < 3; i++) { System.out.print(point[i] + ","); } System.out.println(); } }

Now this loop prints out the same array 10 times. The values in the array are the last ones set at the end of the previous loop.

To fix the problem, you simply need to be sure to create 10 different arrays.

One last issue: If you declare it as Iterator<Integer[]> it (or Iterator<int[]> it), you do not need to cast the return value of it.next(). In fact this is preferred because it is type-safe.

Finally, I want to ask what the ints in each array represent? You might want to revisit your program design and create a class that holds these three ints, either as an array or as three member variables.

这篇关于在Java中的整数数组的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆