2

I have a problem to declare a mulitple arraylist. ... missing the correct syntax or mixed up my mind ...

This works fine till here, to declare an arraylist with an array:

Integer[] arrArray = new Integer[] {1,2,3,4,5};
ArrayList<Integer> arrList = new ArrayList<Integer>();
arrList.addAll(Arrays.asList(arrArray));

also initailising and declaring in one line :

Integer[] arrArray = new Integer[] {1,2,3,4,5};
ArrayList<Integer> arrList = new ArrayList<Integer>(){{
addAll(Arrays.asList(arrArray));}};

or by element :

ArrayList<Integer> arrList = new ArrayList<Integer>(){{
add(1);
add(2);
add(3);
}};

But now I would like to do the same thing, but now with multiple dimension arraylists (and Strings in this case):

private String[] arrFUSE={"3", "true", "0", "FUSE"};
private String[] arrPLACE={ "2", "true", "7", "PLACE"};
private ArrayList<ArrayList<String>> arrLIST = new ArrayList<ArrayList<String>>(){{
add( Arrays.asList(arrFUSE) );
add( Arrays.asList(arrPLACE) );
}};

An other try

private ArrayList<ArrayList<String>> arrLIST = 
        new ArrayList<ArrayList<String>>(){{
            add(     
                  new ArrayList<String>(){{ addAll(Arrays.asList(arrFUSE))}};
            );
}};

Thanks on beforehand.

2 Answers 2

2

Try casting asList result to ArrayList since asList return of type List

ArrayList<ArrayList<String>> arrLIST = new ArrayList<ArrayList<String>>(){{
    add( (ArrayList<String>) Arrays.asList(arrFUSE) );
    add( (ArrayList<String>) Arrays.asList(arrPLACE) );
    }};

or change declaration to List

ArrayList<List<String>> arrLIST = new ArrayList<List<String>>(){{
    add( Arrays.asList(arrFUSE) );
    add( Arrays.asList(arrPLACE) );
    }};

Since you are implementing anonymous inners class right-away, there is no way to find out it's type which is present in outside of it's scope. That's where casting needed.

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

2 Comments

but usually is not neccessary to cast from List to ArrayList. List<String> list = new ArrayList<String>() is ok... why casting in this case ???
@JordiCastilla Edited the answer.
1
String[] arrFUSE={"3", "true", "0", "FUSE"};                            
String[] arrPLACE={ "2", "true", "7", "PLACE"};                         
List<List<String>> arrLIST = new ArrayList<List<String>>();             
arrLIST.add( Arrays.asList(arrFUSE) );                                  
arrLIST.add( Arrays.asList(arrPLACE) );

Don't use { on the line you create a new instance of the ArrayList. If you do you will be writing your own implementation of ArrayList on the lines below it. In stead you just want to use the standard ArrayList so terminate that line and start a new one.

I also changed ArrayList<ArrayList<String>> to List<List<String>> because you want to be able to put any List in there, not just ArrayLists. In fact what Arrays.asList creates is not an ArrayList so it wouldn't fit otherwise.

Comments

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.