0

I'm trying to learn how to use an ArrayList of Arrays. I think I understand the arrays well enough but I'm struggling with that next dimension.

You can see from my code below that I'm just testing the concepts. I've got the exampleArrayList working but I can't figure out exampleArrayListofArrays.

public void exampleArrayList () {
        ArrayList<String> al = new ArrayList<String>();
        al.add("AZ");
        al.add("BY");
        al.add("CX");
        al.add("DW");
        al.add("EV");
        al.add("FU");
        al.add("GT");

        System.out.println("Index of 'AZ': "+al.indexOf("AZ"));
        System.out.println("Index of 'FU': " + al.indexOf("FU"));
        System.out.println("Index of 'AA': " + al.indexOf("AA"));
        System.out.println("Index of 'CX': " + al.indexOf("CX"));

        for (String row : al){
            System.out.println("Value at Index " + al.indexOf(row) + " is " + al.get(al.indexOf(row)));
        }
    }

    public void exampleArrayListofArray () {
        ArrayList<String []> al = new ArrayList<String []>();
        al.add(new String[] {"AB","YZ"});
        al.add(new String[] {"CD","WX"});
        al.add(new String[] {"EF","UV"});
        al.add(new String[] {"GH","ST"});
        al.add(new String[] {"IJ","QR"});
        al.add(new String[] {"KL","OP"});


        System.out.println("Index of 'AB': "+al.indexOf("AB"));
        System.out.println("Index of 'YZ': "+al.indexOf("YZ"));
        System.out.println("Index of 'AA': "+al.indexOf("AA"));
        System.out.println("Index of 'IJ': "+al.indexOf("IJ"));

        for (String [] row : al){
        for (int column = 0; column < 2 ; column ++);
        System.out.println("Value at Index Row" + row + "Column " 
          + column + " is " + al.get(al.indexOf(row)[column]));
        }
    }

I have two areas of understanding that I need to fill out.

1) How can I print out the ArrayList index and the corresponding Array index for the two letters. It seems I need a loop but wasn't sure how to proceed. I also want to know when the letters are not there such as "AA" which I assume will return an index of -1.

Additionally I'd like to be able to print out the whole ArrayList of Array and for some reason my println is not recognizing the int column.

2) for the exampleArrayList method I tried to print out the value at the specified index using this code first:

System.out.println("Value at Index " + al.indexOf(row) + " is " + al.get(row));

And I got an error:

'get(int)' in 'java.util.ArrayList' cannot be applied to '(java.lang.String)'

And I don't understand why.

Thanks for your help with these newbie questions.

Airfix

3 Answers 3

1
al.indexOf(row)[column] returns a String.

The get method want an int. So the error is obvious.

Moreover:

       for (String [] row : al){
        for (int column = 0; column < 2 ; column ++);
        System.out.println("Value at Index Row" + row + "Column " 
          + column + " is " + al.get(al.indexOf(row)[column])); // column here is not define!!!
        }

Try this:

          for (String [] row : al)
            for (int column = 0; column < 2 ; column ++){
            System.out.println("Value at Index Row" + row + "Column " 
              + column + " is " + row[column]); 
            }
Sign up to request clarification or add additional context in comments.

Comments

0

Specific item can be printed using row[column]

for (String [] row : al){
    for (int column = 0; column < 2 ; column ++);
    System.out.println("Value at Index Row" + row + "Column " 
      + column + " is " + row[column]);
    }
}

As you already got the array instance at an index in outer for loop. Now you can just print the values in that array by using column index.

System.out.println("Value at Index " + al.indexOf(row) + " is " + al.get(row));

is giving you error at al.get(row) because ArrayList.get method accepts an index that is int value.

Hope this helps.

Comments

0

Arrays and ArrayLists are not meant for reverse search. It is possible to run through them with a loop and test all values, but it is extremely inefficient. Instead, try a HashMap.

Here, you can store an index for a string and ask for it later.

2 Comments

I'm not familiar with the capabilities of HashMap. Ultimately what I want my final code to do is this: A)Populate an ArrayList (or HashMap) from a txt file from an asset file (about 100 lines long by 3 columns). B) Based on user input text I want to search the arrayList column 1 for the text and return the values in column 2 and 3 for some calculations. C) Then if the user string is not found I want the user to be able to create the values in columns 2 and 3 save the data to the ArrayList (HashMap), sort the ArrayList by alphabetical order then save the Array back to the asset text file.
You could define a new class RowColumn with two int values that contains the information of column 2 and 3. Then you can define a HashMap<String,RowColumn> and put elements in it (using the put command). By get, you can get the RowColumn for each String. For reading and writing the File linewise you can use FileUtils of apache (or some Java 7 features of the Files class).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.