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

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

问题描述

大家.

我刚开始接触 Java,我正在尝试编写一个简单的游戏,其中敌人在网格上追逐玩家.我正在使用维基百科寻路页面上的简单算法进行寻路.这涉及创建两个列表,每个列表项包含 3 个整数.这是我正在尝试构建和显示这样一个列表的测试代码.

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.

当我运行以下代码时,它会为 ArrayList 中的每个数组打印出相同的数字.为什么要这样做?

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.

接下来,将变量声明为 List;list 优于将其声明为 ArrayList.这允许您轻松地将 List 更改为 LinkedList 或其他一些实现,而不会破坏其余代码,因为它保证仅使用 中可用的方法列表界面.有关更多信息,您应该研究编程接口".

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];

这一行显然创建了一个 Integer 数组.

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);
    //...
}

在这里,您为数组的元素赋值,然后向List 中的数组添加引用.每次循环迭代时,您将新值分配给同一个数组,并将对同一个数组的另一个引用添加到List.这意味着 List 有 10 个引用对同一个数组已被重复重写.

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.

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

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(); } }

现在这个循环打印出相同的数组 10 次.数组中的值是在上一个循环结束时设置的最后一个值.

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.

要解决此问题,您只需确保创建 10 个不同的数组.

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

最后一个问题:如果你将 it 声明为 Iterator;it(或Iterator it),你不需要转换it.next()的返回值.事实上,这是首选,因为它是类型安全的.

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.

最后想问一下每个数组中的int代表什么?您可能想要重新审视您的程序设计并创建一个类来保存这三个 int,作为数组或三个成员变量.

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天全站免登陆