Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayList and String[] AND Object[]

Tags:

java

I have searched this, but couldn't find what i need, so i created a new post. I hope to understand about this problem. Thanks.

ArrayList<String> arraylist= new ArrayList<String>();
arraylist.add("Nguyen");
arraylist.add("Viet");

String[] name={"Quan","Doan","Thi","Ha"};
arraylist.add(name);// error here 

ArrayList<Object> arraylist1=new ArrayList<Object>();
arraylist1.add("Nguyen");
arraylist1.add("Viet");
Object[] name1={"Quan","Doan","Thi","Ha"};
arraylist1.add(name1);// not error

Can someone explain that when i pass name into add() method then I get a error, but when i pass name1 into add() method, it works fine, why is it so...

like image 268
Quan Nguyen Avatar asked Jul 29 '15 08:07

Quan Nguyen


People also ask

What is the difference between String [] and list String Java?

An array String[] cannot expand its size. You can initialize it once giving it a permanent size: String[] myStringArray = new String[20](); myStringArray[0] = "Test"; An ArrayList<String> is variable in size.

What is the difference between String array and ArrayList?

Array is a fixed length data structure whereas ArrayList is a variable length Collection class. We cannot change length of array once created in Java but ArrayList can be changed. We cannot store primitives in ArrayList, it can only store objects. But array can contain both primitives and objects in Java.

Is ArrayList only String?

arraylist is an ArrayList of String elements, so you can't add an array instance to it. arraylist1 is an ArrayList of Object elements, so you can add an array to it, since an array is an Object .

What is the difference between list String and ArrayList String?

The List is an interface, and the ArrayList is a class of Java Collection framework. The List creates a static array, and the ArrayList creates a dynamic array for storing the objects. So the List can not be expanded once it is created but using the ArrayList, we can expand the array when needed.


2 Answers

arraylist is an ArrayList of String elements, so you can't add an array instance to it. arraylist1 is an ArrayList of Object elements, so you can add an array to it, since an array is an Object.

If you wish to add the individual elements of the arrays to the Lists, both add calls should be changed to addAll :

arrayList.addAll(Arrays.asList(name));
arraylist1.addAll(Arrays.asList(name1));
like image 132
Eran Avatar answered Sep 25 '22 15:09

Eran


arraylist.add(name);// error here 

Error because name is an array. Not a String. You are trying to add a Array object to an ArrayList which accepts only Strings.

arraylist1.add(name1);// not error

No error because name1 is an Object array. In Java every class is an Object, even an array of Objects also an Object. Hence it accepted it as an Object. Though your name1 is an array of Objects, as a whole it is an Object itself first.

like image 8
Suresh Atta Avatar answered Sep 21 '22 15:09

Suresh Atta