2

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):

        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:

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?

Andrew
  • 175
  • 1
  • 2
  • 12

4 Answers4

5

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);
Mike B
  • 5,390
  • 2
  • 23
  • 45
  • Why doesn't the data look like it belongs in a 2d array/list? My first example(the data that I'm actually working with) has multiple data per row. Lets say row 5 is manufacturer numbers for said item and we get the item from two different manufacturers depending on price. In the original database it's stored in the same row but delimited. I'm trying to package the data for easy use in my programs so if I want the second manufacturer's number I say info[5][1] or now, info.get(5).get(1). EDIT: not trying to sound defensive, just wondering if there was a better solution! – Andrew Aug 04 '14 at 18:57
  • 1
    So if that data represents stuff relating to an item, then make an `Item` class with a property that is a list of manufacturer numbers. Then instead of `info[5][1]`, which is not very descriptive, you could do `item.getManufacturerNumber(2)` or `item.getManufacturerNumbers()[1]` or something similar. That makes it clear what the data represents. – Mike B Aug 04 '14 at 21:20
2

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

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

Then you can add ArrayList<String>s to 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.

String myFavorite = cheese.get(0).get(2);
rgettman
  • 176,041
  • 30
  • 275
  • 357
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:

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.

AdamMc331
  • 16,492
  • 10
  • 71
  • 133
0

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

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