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