Skip to content
Advertisement

How to set arrayList size as null?

String a []= {null,null,null,null,null};

//add array to arraylist
ArrayList<Object> choice = new ArrayList<Object>(Arrays.asList(a)); 

System.out.println(choice.size());

Why the size of the arrayList choice is 5 when all the elements have been set to null

Advertisement

Answer

Because the ArrayList still contains 5 Objects. They are null, but they are objects, thus present in the list!

You can empty the ArrayList, by calling choice.clear();

Advertisement