Java 2d数组还是2d ArrayList?

时间:2022-06-02 02:31:31

I am tasked with making a program that interfaces with a multivalue database. I receive the entire record in a string with delimeters for the values and subvalues. From there I split the data into a 2d array. Here's a snippet of one (notice the varying lengths):

我的任务是创建一个与多值数据库接口的程序。我以带有值和子值的delimeter的字符串接收整个记录。从那里我将数据分割成一个二维数组。这里有一个片段(注意不同的长度):

        0           1           2
   +------------------------------------+
0  | 442        |           |           |
1  | C          |           |           |
2  |            |           |           |
3  | N          |           |           |
4  | LAND       |           |           |
5  | 05412300   | 05412307  |           |
6  | BEARING    |           |           |
7  | BRAND1     | BRAND2    | BRAND3    |
8  | 12         |           |           |
   +------------------------------------+

Now I've always used ArrayLists instead of Arrays because I knew it was best practice. Now I'm wondering if using a 2d ArrayList would be beneficial as well. I've looked at some examples and I don't quite understand how to get the values when I need them. If I were to do:

现在我总是使用arraylist而不是数组,因为我知道这是最佳实践。现在我想知道是否使用2d ArrayList也是有益的。我看过一些例子,我不太明白当我需要这些值时该如何去获取它们。如果我这样做:

String[][] cheese = {
    {"chedder", "swiss", "pepperjack"},
    {"sliced", "cubed", "block"}
};
String myFavorite = cheese[0][2];

myFavorite would be pepperjack, but how would I do that with a 2d ArrayList? The example's I've found haven't been too helpful for me. I do know the exact number of rows (but not columns) of data I'll be receiving and the row number will not change so maybe a regular 2d array would be best practice?

我最喜欢的是pepperjack,但是我怎么用2d ArrayList呢?我发现的例子对我来说没有太大帮助。我确实知道我将要接收的数据的确切行数(但不知道列数),而且行数不会改变,所以也许常规的2d数组是最佳实践?

4 个解决方案

#1


3  

Your data doesn't look like it belongs in a 2d array or list. If it were me I would create data structures that make sense for the data. If you must use a list of lists:

你的数据看起来不像是属于二维数组或列表。如果是我,我会创建对数据有意义的数据结构。如果你必须使用列表:

List<List<String>> cheese = new ArrayList<List<String>>();
cheese.add(Arrays.asList("cheddar", "swiss", "pepperjack"));
cheese.add(Arrays.asList("sliced", "cubed", "block"));

String myFavorite = cheese.get(0).get(2);

#2


2  

You can make a 2D ArrayList by making the members of your outer ArrayList themselves ArrayLists.

你可以制作一个2D ArrayList,让你的外部ArrayList的成员自己成为ArrayList。

List<List<String>> cheese = new ArrayList<List<String>>();

Then you can add ArrayList<String>s to cheese.

然后可以将ArrayList s添加到cheese中。

favorites.add(new ArrayList<String>(Arrays.asList("cheddar", "swiss", "pepperjack")));
favorites.add(new ArrayList<String>(Arrays.asList("sliced", "cubed", "block")));

You can extract the elements with two successive get calls.

您可以使用两个连续的get调用来提取元素。

String myFavorite = cheese.get(0).get(2);

#3


0  

You can use the get function just like a single dimensional ArrayList. So, if your ArrayList is cheese, that has additional ArrayLists you can try something like this:

你可以像使用一维数组列表一样使用get函数。所以,如果你的ArrayList是cheese,它有更多的ArrayList你可以试试这样的东西:

String myFavorite = cheese.get(0).get(2);

The first get will return the first ArrayList, and the second get will return the third item (second index) of said first ArrayList.

第一个get将返回第一个ArrayList,第二个get将返回第一个ArrayList的第三个项(第二个索引)。

#4


0  

I don't have an example, but you should be able to make an ArrayList of ArrayLists and do something like this:

我没有一个例子,但是你应该可以制作一个ArrayList这样的东西

String myFavorite = cheese.get(0).get(2);

#1


3  

Your data doesn't look like it belongs in a 2d array or list. If it were me I would create data structures that make sense for the data. If you must use a list of lists:

你的数据看起来不像是属于二维数组或列表。如果是我,我会创建对数据有意义的数据结构。如果你必须使用列表:

List<List<String>> cheese = new ArrayList<List<String>>();
cheese.add(Arrays.asList("cheddar", "swiss", "pepperjack"));
cheese.add(Arrays.asList("sliced", "cubed", "block"));

String myFavorite = cheese.get(0).get(2);

#2


2  

You can make a 2D ArrayList by making the members of your outer ArrayList themselves ArrayLists.

你可以制作一个2D ArrayList,让你的外部ArrayList的成员自己成为ArrayList。

List<List<String>> cheese = new ArrayList<List<String>>();

Then you can add ArrayList<String>s to cheese.

然后可以将ArrayList s添加到cheese中。

favorites.add(new ArrayList<String>(Arrays.asList("cheddar", "swiss", "pepperjack")));
favorites.add(new ArrayList<String>(Arrays.asList("sliced", "cubed", "block")));

You can extract the elements with two successive get calls.

您可以使用两个连续的get调用来提取元素。

String myFavorite = cheese.get(0).get(2);

#3


0  

You can use the get function just like a single dimensional ArrayList. So, if your ArrayList is cheese, that has additional ArrayLists you can try something like this:

你可以像使用一维数组列表一样使用get函数。所以,如果你的ArrayList是cheese,它有更多的ArrayList你可以试试这样的东西:

String myFavorite = cheese.get(0).get(2);

The first get will return the first ArrayList, and the second get will return the third item (second index) of said first ArrayList.

第一个get将返回第一个ArrayList,第二个get将返回第一个ArrayList的第三个项(第二个索引)。

#4


0  

I don't have an example, but you should be able to make an ArrayList of ArrayLists and do something like this:

我没有一个例子,但是你应该可以制作一个ArrayList这样的东西

String myFavorite = cheese.get(0).get(2);