0

Here is my program

ArrayList<ArrayList<?>> matrix = new ArrayList(); // nested list so as to have each matrix.add(new ArrayList()); matrix.add(new ArrayList()); matrix.add(new ArrayList()); 

I know I can access each item of the list by using matrix.get(0) but what if I want to access the first item of the first nested list (I hope it is clear) That is what I would like to do. It would even be better if I could turn each of the nested list or even the entire list into a proper array with nested arrays inside but that may be a tall order. I am sorry my programming skills are really poor.

2
  • 1
    What about matrix.get(0).get(0)? Commented Feb 26, 2014 at 21:16
  • Why is matrix.get(0).get(0) bad? Commented Feb 26, 2014 at 21:17

5 Answers 5

1

To access the first item of the first list you could do matrix.get(0).get(0);. When you do matrix.get(0) that returns the first List, which you can then do more operations on (get, add, remove, etc.) which is no different then something like aList.get(0)

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

Comments

0
<?> foo = matrix.get(0).get(0); 

Comments

0

You just need to refer to the first element of the first element

matrix.get(0).get(0) 

Comments

0

You could use something like this

public static void main(String[] args) throws Exception { ArrayList<ArrayList<?>> matrix = new ArrayList<ArrayList<?>>(); ArrayList<Integer> al = new ArrayList<Integer>(); al.add(1); matrix.add(al); ArrayList<Integer> al2 = new ArrayList<Integer>(); al2.add(2); al2.add(3); matrix.add(al2); Object[] objs = new Object[al.size()]; objs = matrix.toArray(objs); System.out.println(java.util.Arrays.toString(objs)); } 

Outputs

[[1], [2, 3]] 

Comments

0

Hope, following code describes everything for you:

//get the first list from matrix List<?> firstNestedList = matrix.get(0); //get the first element from the firstNestedList Object neededElement = firstNestedList.get(0); 

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.