0

I'm new to Java and I'm trying to learn how to parse through an ArrayList within an ArrayList and I can't quite figure it out. I'm used to Python where all you had to do was list[index][index]. Why am I getting an error reading Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any> when trying to use list.get(index).get(index)? Is this not the proper syntax?

    import java.io.*;
    import java.util.*; 
    public class Practice {
        public static void main(String[] args){
            ArrayList list = new ArrayList(Arrays.asList(new Integer[]{1,2,3,4,5,6,7,8,9,10})); 
            ArrayList list1 = new ArrayList(Arrays.asList(new Integer[]{1,2,3,4,5,6,7,8,9,10})); 
            list.add(list1); 
            System.out.println(list.get(10).get(0));

        }

    }

2 Answers2

3

Java and Python are quite different when it comes to types: Java Types vs Python Types

Java requires explicit type declarations and is very strict on how types are used. For example, you need to explicitly specify what type of ArrayLists you are using.

Assuming that you wanted to create 2 ArrayLists, outerList that contains innerLists that each contain the numbers 1-10, this is Java code will do the trick:

import java.io.*;
import java.util.*; 
public class Practice {
    public static void main(String[] args) {
         ArrayList<Integer> innerList = new ArrayList<Integer>(Arrays.asList(new Integer[]{1,2,3,4,5,6,7,8,9,10}));
         ArrayList<ArrayList<Integer>> outerList = new ArrayList<ArrayList<Integer>>(); 
         for (int i = 0; i < 10; i++) {
             outerList.add(innerList); 
         } 
         System.out.println(outerList.get(9).get(0));
    }
}
DeveloperDemetri
  • 208
  • 2
  • 10
  • Thanks this does work, but why doesn't it work with my code I originally posted? – Anonymous 23234720-12473 Feb 13 '16 at 00:49
  • 1
    @Swanson7 You probably should read [What is a raw type and why shouldn't we use it?](http://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – Pshemo Feb 13 '16 at 00:53
2

Try using this instead:

list.addAll(list1);

John Harris
  • 322
  • 1
  • 9
  • OP seems to be aware of fact what `list.add(list1)` does since he explicitly is trying to call `list.get(10).get(0)` so that is not the problem. Problem is with second `get(0)` since compiler can't safely assume that first `get(10)` returns ArrayList. – Pshemo Feb 13 '16 at 00:10
  • In other words this answer can't make this code `list.get(10).get(0)` work. – Pshemo Feb 13 '16 at 00:18
  • @Pshemo That is correct, How would I go about calling the inner list? – Anonymous 23234720-12473 Feb 13 '16 at 00:42
  • @Swanson7 You could tell compiler that you are sure that item on index `10` is ArrayList. You could do it by casting result of `get(10)` to ArrayList so compiler would let you use its `get(0)` method like `((ArrayList)list.get(10)).get(0)` but I suspect that you better avoid it. You should probably create your own class which could store `list` and `list1` as separate fields (I don't know what you want to achieve so I can't tell you how it should look precisely). You may also need to create your own Tree implementation (it is really hard to tell without more context). – Pshemo Feb 13 '16 at 00:46