0

I'm having trouble figuring outa few things about the following code:

public final String[][] pockets = {{"STATUS CHANGERS", "RUNES", "KEY ITEMS", "TROPHIES"},
        {"POTION", "SUPER POTION", "FULL HEAL"},{"ARMOR+","ATTACK+","EXP+","HEALTH+", "DISPELL+"},
        {"QUEST ITEMS","STORY ITEMS","JOURNAL"},{TROPHIES}};


public final List<String[]> Pockets = Arrays.asList(pockets); //why couldn't I define it in the method, I wonder?

I am trying to figure out why I could not define the 2d Array in the asList() method like you might a normal Array. Also, I know there has to be a Java subclass to import or a more semantic approach.

1
  • 3
    Please clarify like you might a normal Array. Commented Dec 4, 2015 at 19:41

3 Answers 3

3

In order to transform a two-dimensional array E[][] into a two-dimensional List<List<E>>, you can use the following code (look below for an explanation):

public final String[][] pocketArray = {{"STATUS CHANGERS", "RUNES", "KEY ITEMS", "TROPHIES"},
        {"POTION", "SUPER POTION", "FULL HEAL"}, {"ARMOR+", "ATTACK+", "EXP+", "HEALTH+", "DISPELL+"},
        {"QUEST ITEMS", "STORY ITEMS", "JOURNAL"}, {"TROPHIES"}};


public final List<List<String>> pocketList;

public Test()
{
    pocketList = new ArrayList<>(pocketArray.length);
    for (String[] pocket : pocketArray)
    {
        List<String> currentSubList = new ArrayList<>(pocket.length);
        // Nice and fast way instead of iterating over the whole array and adding the strings to the list.
        Collections.addAll(currentSubList, pocket);
        // instead, you could do something like this:
        //
        // for (String currentString : pocket)
        // {
        //     currentSubList.add(currentString);
        // }
        //
        pocketList.add(currentSubList);

        // Alternatively, you could use something like this:
        // pocketList.add(Arrays.asList(pocket));
    }
}

Basically, this code is creating a new ArrayList<List<String>> for holding all the sub-lists later. Then it iterates over the array, and saves all the sub-arrays into the list.


By the way, names like pockets versus Pockets are prone to causing confusion later, I recommend you to start field names with lowercase letters and name them uniquely, by their purpose and possibly their type, like I did above.

Sign up to request clarification or add additional context in comments.

Comments

3

Is this what you are trying to do? Notice the use of new String[][]

 public final List<String[]> Pockets = Arrays.asList(new String[][]{{"STATUS CHANGERS", "RUNES", "KEY ITEMS", "TROPHIES"},
        {"POTION", "SUPER POTION", "FULL HEAL"},{"ARMOR+","ATTACK+","EXP+","HEALTH+", "DISPELL+"},
        {"QUEST ITEMS","STORY ITEMS","JOURNAL"},{"TROPHIES"}});

PS: Pockets is not a standard naming convention, I suggest pocketList

Comments

0

I am trying to figure out why I could not define the 2d Array in the asList() method like you might a normal Array.

We cannot answer why what you tried did not work when you do not tell us what you tried. I suppose what you really want to know, however, is how to generate a List<String[]> via Arrays.asList() by passing the individual elements, instead of first constructing a 2D array. That would be this:

public final List<String[]> pockets = Arrays.asList(
        new String[]{"STATUS CHANGERS", "RUNES", "KEY ITEMS", "TROPHIES"},
        new String[]{"POTION", "SUPER POTION", "FULL HEAL"},
        new String[]{"ARMOR+","ATTACK+","EXP+","HEALTH+", "DISPELL+"},
        new String[]{"QUEST ITEMS","STORY ITEMS","JOURNAL"},
        new String[]{"TROPHIES"}
    );

Note in particular that Java has no array literals, only array initializers (which can be associated with array declarations), and array constructors, such as in the above code. I speculate that in your attempt(s), you omitted the new String[] bits that make those constructors. Those bits are necessary to specify the element type of the arrays, if for no other reason.

Also, I know there has to be a Java subclass to import or a more semantic approach.

I've no idea what you mean. You're already using class java.util.Array to perform the construction, and you're building a java.util.List. I guess for most purposes I'd recommend using a List of Lists (List<List<String>>) and avoiding arrays altogether, but I can't be sure whether that would be suitable for your purposes.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.