简体   繁体   中英

ArrayList and String[] AND Object[]

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...

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 List s, both add calls should be changed to addAll :

arrayList.addAll(Arrays.asList(name));
arraylist1.addAll(Arrays.asList(name1));
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.

You seem to be thinking that when you do

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

It is supposed to add all the elements of the array to the list. That's not true. The add method just adds the one parameter it is given to the list.

In the first case, that one element is an array of strings. An array of strings is not a string itself, so this fails.

In the second case, that one element is an array of objects. An array of objects is itself an object, and therefore it is added to the list. But note that the array is added, not the objects inside it.

1st example throws an error because you declare an ArrayList of Strings but later try to add an Array of Strings (not a String , but a collection of Strings ) to it.

2nd example works because you declare and ArrayList of Objects and later you add Objects to it (understand that everything in Java is an Object : a String is an Object , an Array also and for example a Boolean is an Object also, you can try adding True in the 2nd example and you will see that it will also work).

Here i see some misunderstanding of String and String[] both are not same, while seeing your following code

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

it seems like you are trying to add String[] object into ArrayList of type String . If you want to add name array into list then your list type must be String[] . So becasue type are different so you cannot add String[] object into list of type String . But When your have list of type Object then of course you can add any object into it.

IDE tells us about object data type clearly.

First code snippet

In this error code snippet:
ArrayList<String> arraylist= new ArrayList<String>();
arraylist.add("Nguyen");
arraylist.add("Viet");
String[] name={"Quan","Doan","Thi","Ha"};
arraylist.add(name);

在此输入图像描述 At line 6, object named arraylist has type ArrayList<java.lang.String> but you try something like this: ArrayList<java.lang.String[]> cause an incompatibility data type error.

Second code snippet

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

在此输入图像描述 When step debug to line 10, name1 is has data type java.lang.Object , arraylist1 has datatype ArrayList<Object> . Therefore, add element name1 to arraylist1 success.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM