数据结构来存储JAVA中2D阵列 [英] Data structure to store 2D-arrays in JAVA

查看:189
本文介绍了数据结构来存储JAVA中2D阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找一个数据结构来存储二维整型数组。
被列出分辩数据结构或者我应该用另外一个?

i am looking for a data structure to store two dimensional integer arrays. Is List the rigth data structure or should i use another one?

有人可以给我如何创建这样的数据结构,以及如何添加一个二维数组?一个简单的例子

Can someone give me a short example on how to create such a data structure and how to add a 2d array?

编辑:我想我在其中要存储INT [11] [7]数组的数据结构。
比如十,INT [11] [7]数组。

I want a data structure in which i want to store int[11][7] arrays. For instance ten, int[11][7] arrays.

推荐答案

如果你需要存储在数据结构中的数 INT [] [] 阵列,我可能会建议您存储在一个对象的 INT [] [] 阵列的再presents什么数据包括,然后存储这些对象的ArrayList

If you need to store a number of int[][] arrays in a data structure, I would probably recommend that you store the int[][] arrays in an Object that represents what the data contains, then store these Objects in an ArrayList.

例如,这里有一个简单的对象包装为你的 INT [] [] 阵列...

For example, here is a simple Object wrapper for your int[][] arrays...

public class 2DArray {
    int[][] array;
    public 2DArray(int[][] initialArray){
        array = initialArray;
    }
}

这是你将如何使用它们,并将它们存储在一个的ArrayList ...

And here is how you would use them, and store them in an ArrayList...

// create the list
ArrayList<2DArray> myList = new ArrayList<2DArray>;
// add the 2D arrays to the list
myList.add(new 2DArray(myArray1));
myList.add(new 2DArray(myArray2));
myList.add(new 2DArray(myArray3));

原因我的建议是,你的 INT [] [] 数组必须有一定的意义给你。通过在对象存储在此包装类,你可以给它一个意思。例如,如果该值分别为坐标,你会打电话给你的类坐标而不是 2DArray 的。因此,您创建一个列表 坐标,这已经超过 int类型的更多的意义[的] [] []

The reason for my suggestion is that, your int[][] array must have some meaning to you. By storing this in an Object wrapper class, you can give it a meaning. For example, if the values were co-ordinates, you would call your class Coordinates instead of 2DArray. You therefore create a List of Coordinates, which has a lot more meaning than int[][][].

这篇关于数据结构来存储JAVA中2D阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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