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));

        }

    }
1
  • Why are you using raw types instead of generics? Commented Feb 12, 2016 at 23:56

2 Answers 2

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));
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks this does work, but why doesn't it work with my code I originally posted?
@Swanson7 You probably should read What is a raw type and why shouldn't we use it?
2

Try using this instead:

list.addAll(list1);

4 Comments

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.
In other words this answer can't make this code list.get(10).get(0) work.
@Pshemo That is correct, How would I go about calling the inner list?
@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).

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.