简体   繁体   English

Java 2d Array或2d ArrayList?

[英]Java 2d Array or 2d ArrayList?

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. 从那里,我将数据分成2d数组。 Here's a snippet of one (notice the varying lengths): 这是一个片段(请注意长度的变化):

        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. 现在,我一直使用ArrayLists而不是Arrays,因为我知道这是最佳实践。 Now I'm wondering if using a 2d ArrayList would be beneficial as well. 现在我想知道使用2d ArrayList是否也将有益。 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: 如果我要这样做:

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? myFavorite将是Pepperjack,但如何使用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? 我确实知道我将要接收的数据的确切行数(但不是列数),并且行数不会改变,所以也许常规的2d数组将是最佳实践?

Your data doesn't look like it belongs in a 2d array or list. 您的数据看起来不属于2D数组或列表。 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);

You can make a 2D ArrayList by making the members of your outer ArrayList themselves ArrayList s. 你可以做一个二维ArrayList通过使你的外部成员ArrayList自己ArrayList秒。

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

Then you can add ArrayList<String> s to cheese . 然后,您可以将ArrayList<String>添加到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);

You can use the get function just like a single dimensional ArrayList. 您可以像使用一维ArrayList一样使用get函数。 So, if your ArrayList is cheese, that has additional ArrayLists you can try something like this: 因此,如果您的ArrayList是奶酪,则它具有其他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的第三项(第二个索引)。

我没有示例,但是您应该能够制作一个ArrayLists的ArrayList并执行以下操作:

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

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

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