简体   繁体   English

Java将2D数组列表添加到2D数组列表的列表中

[英]Java adding a 2D arraylist to a list of 2D arraylists

I am creating a map system for my game which requires me at a few stages to replace the old 2D array map with another. 我正在为自己的游戏创建一个地图系统,这需要我在几个阶段中用另一个替换旧的2D阵列地图。 What I wanted to do is add all the 2D arraylist maps into a List of these 2D arraylists. 我想要做的是将所有2D arraylist映射添加到这些2D arraylist的列表中。 However, what I found difficult was that when I wanted to retrieve said map, the .get method would not give me the correct data, as it you would try to pull the first item of that arraylist which in this case would be null. 但是,我发现困难在于,当我想检索所述映射时,.get方法无法提供正确的数据,因为您将尝试提取该arraylist的第一项,在这种情况下将为null。 Thus the program threw me nullpointerexceptions. 因此,该程序使我抛出了nullpointerexceptions。

private int[][] mapArray;
private List<int[][]> maps = new ArrayList<>();

    mapArray = new int[][]{
    {0, 0, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0},
    {0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 1, 9, 1, 1, 7, 7, 7, 7, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {7, 7, 7, 7, 5, 8, 8, 8, 4, 6, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {4, 4, 4, 4, 4, 8, 8, 8, 4, 4, 6, 7, 7, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {4, 4, 4, 4, 4, 8, 8, 8, 4, 4, 4, 4, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
   };
   maps.add(mapArray);

When I sout maps.get(0) I get the following: 当我发出maps.get(0)时,我得到以下信息:

[[I@58838f3f

If I have two items, it will show up like this: 如果我有两个项目,它将显示如下:

[[[I@58838f3f, [[I@59a60155]

Ideally I want it to be added like this: 理想情况下,我希望这样添加它:

[I@58838f3f, I@58838f3f]

I am just unsure how to do this. 我只是不确定如何执行此操作。 Any help would be appreciated! 任何帮助,将不胜感激!
I might have overlooked something obvious, probably is the case! 我可能忽略了一些显而易见的事情,也许是这样!

Much obliged 多谢

You seem to be confusing the output of converting your map (or the list of maps) to string (which is what happens when you try to print them) - with what actual value there are there. 您似乎使将地图(或地图列表)转换为字符串(这是您尝试打印它们时发生的事情)的输出与实际值混淆了。

Because ArrayList like most Java container formats have nice toString() implementations that get automatically called when trying to print them, you'd get something that "looks like an array" in the output. 由于ArrayList像大多数Java容器格式一样,具有很好的toString()实现,在尝试打印它们时会自动调用它们,因此您在输出中会得到“看起来像数组”的内容。 So for example when printing an array list containing two strings, it might look like this: [a, b] . 因此,例如,当打印包含两个字符串的数组列表时,它可能看起来像这样: [a, b]

Arrays don't have such nice formatters, because they aren't really objects (they are "primitive types") and so print them gets you their address and some Java byte code that describes their type (if you can understand Java byte code), so an array of integers will look like this: [I@<address> while an two dimensional array of bytes will look like this: [[B@<address> , and so on. 数组没有很好的格式化程序,因为它们不是真正的对象(它们是“原始类型”),因此打印它们可获得地址和描述其类型的一些Java字节码(如果您能理解Java字节码) ,因此整数数组将如下所示: [I@<address>而二维字节数组将如下所示: [[B@<address> ,依此类推。

In regards to your original question, the code seems to work well for me. 关于您的原始问题,该代码对我来说似乎很好。 Here is an example program: 这是一个示例程序:

import java.util.ArrayList;

public class Test2D {

    public static void main(String... args) {
        ArrayList<int[][]> maps = new ArrayList<>();
        maps.add(new int[][] {
            {1,2,3},
            {4,5,6},
            {7,8,9},
        });
        maps.add(new int[][] {
            {10,20,30},
            {40,50,60},
        });
        int[][] map = maps.get(0);
        System.out.println(map[0][0]);
    }
}

The output will print correctly 1 , showing that there is no null pointer exception. 输出将正确打印1 ,表明没有空指针异常。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM