kamal8878
kamal8878

Reputation: 5

How to resolve nested arraylist output in java

As per some condition i add some values(it may single or multiple values) in my Arraylist.

in common method:

List<Object> listsALL.....

listsALL.add(sItems2); 

value will return to calling method.

i can access values below way

sItems100.add(String.valueOf(listsALL.get(3)));

output like

sItems100---------------- [[Item 5, Item 7]]

While fetching the value from sItems100 and add into nested list, i'm getting below output

[[Item 1, Item 2, Item 6], [[Item 5, Item 7]]]

but i expect like

[[Item 1, Item 2, Item 6], [Item 5, Item 7]]

How to resolve this, any one can help in this?

Upvotes: 0

Views: 1027

Answers (2)

Lucian
Lucian

Reputation: 71

It is not a great idea to keep a List of generic Objects of different types (e.g. List in your case), and to insert both an singular Object and a List in this initial list.

If it is possible, try using a

List<List<Object>>

instead. Don't forget that List is an interface, implemented below by ArrayList class.

     ArrayList<ArrayList<String>> aList = new ArrayList<ArrayList<String>>();         

     ArrayList<String> firstElement = new ArrayList<String>();
     firstElement.add("Object1");

     ArrayList<String> secondElement = new ArrayList<String>();
     secondElement.add("Object2");
     secondElement.add("Object3");         

     aList.add(firstElement);
     aList.add(secondElement);

     System.out.println(aList.get(0).get(0));
     System.out.println(aList.get(1).get(0));
     System.out.println(aList.get(1).get(1));

Here you have two similar Lists in the initial aList, one with only one element, one with two elements.

If you want to use both an Object and a List inside your initial list, you might want to check how to use instanceof operator and casting in Java.

if(aList.get(1) instanceof ArrayList){
         System.out.println("True");
     }
     else {
         System.out.println("False");
     }

Here is the full example of using instanceof:

 ArrayList<Object> aList = new ArrayList<Object>();         

 String firstElement = "Object1";

 ArrayList<String> secondElement = new ArrayList<String>();
 secondElement.add("Object2");
 secondElement.add("Object3");         

 aList.add(firstElement);
 aList.add(secondElement);

 for (int i = 0; i < aList.size(); i++){
     if (aList.get(i) instanceof String ){
         System.out.println( (String) aList.get(i) );
     } 
     if (aList.get(i) instanceof ArrayList){
         System.out.println( ((ArrayList<String>)aList.get(i)).get(0) );
     }         
 }   

So the initial aList holds two types of Objects in this case, String and ArrayList. When iterating the list, you use instanceof operator to see what type of Object it is, and cast it accordingly.

Upvotes: 0

Lewis Wong
Lewis Wong

Reputation: 269

Do you want something like this?

List<Object> listsALL = new ArrayList<Object>();

List<Integer> list1 = new ArrayList<Integer>();
list1.add(1);
list1.add(2);
list1.add(6);

List<Integer> list2 = new ArrayList<Integer>();
list2.add(5);
list2.add(7);

listAll.add(list1);
listAll.add(list2);

Upvotes: 1

Related Questions