在JAVA中存储二维数组的数据结构 [英] Data structure to store 2D-arrays in JAVA

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

问题描述

我正在寻找一种数据结构来存储二维整数数组.List 是正确的数据结构还是我应该使用另一个?

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[][] 数组在 Object 中表示数据包含的内容,然后将这些 Objects 存储在 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[][] 数组的简单 Object 包装器

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[][] 数组对你来说一定有一定的意义.通过将其存储在 Object 包装类中,您可以赋予它一个含义.例如,如果值是坐标,您将调用您的类 Coordinates 而不是 2DArray.因此,您创建了一个 CoordinatesList,它比 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中存储二维数组的数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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