15

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

1
  • 7
    because an Object[] is an Object Commented Jul 29, 2015 at 16:59

6 Answers 6

22

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));
Sign up to request clarification or add additional context in comments.

Comments

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

4 Comments

Does not every type instance in Java derive from Object? Are there any caveats where one could NOT add a referenced value to arraylist1?
@ShaunWilson Primitives does not derive from Object. You could still add them to arrayList1 because they would be autoboxed, but they would not be primitives anymore. Also, the term everything is too large. An operator is not an object in java while it may be in other languages which causes the confusion.
@Jean-FrançoisSavard My bad.. should be Every class. Thanks for pointing out.
@ShaunWilson There is no caveats at all. You can add anytype of Object or Primitive to it. If you see there are only Classes and primitives. Since the arraylist of type Object, you can add any classes object to it. Coming to primitive part, from Java 1.5 we have autoboxing/unboxing which automatically turns any primitive to its equivalent Wrapper class.
6

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.

Comments

5

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

Comments

2

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.

Comments

1

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);

enter image description here 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);

enter image description here 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.

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.