[英]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... 有人可以解释当我将
name
传递给add()
方法然后我得到一个错误,但是当我将name1
传递给add()
方法时,它工作正常,为什么会这样...
arraylist
is an ArrayList of String elements, so you can't add an array instance to it. arraylist
是String元素的ArrayList,因此您无法向其添加数组实例。 arraylist1
is an ArrayList
of Object
elements, so you can add an array to it, since an array is an Object
. arraylist1
是Object
元素的ArrayList
,因此您可以向其添加数组,因为数组是Object
。
If you wish to add the individual elements of the arrays to the List
s, both add
calls should be changed to addAll
: 如果您希望将数组的各个元素添加到
List
,则两个add
调用都应更改为addAll
:
arrayList.addAll(Arrays.asList(name));
arraylist1.addAll(Arrays.asList(name1));
arraylist.add(name);// error here
Error because name
is an array. 错误,因为
name
是一个数组。 Not a String
. 不是
String
。 You are trying to add a Array object to an ArrayList which accepts only Strings. 您正在尝试将Array对象添加到仅接受字符串的ArrayList。
arraylist1.add(name1);// not error
No error because name1
is an Object
array. 没有错误,因为
name1
是一个Object
数组。 In Java every class is an Object, even an array of Objects also an Object . 在Java中, 每个类都是一个Object,甚至一个Object数组也是一个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.
虽然你的name1是一个Objects数组,但作为一个整体,它首先是一个Object。
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. add
方法只是将它给出的一个参数添加到列表中。
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. 第一个示例抛出一个错误,因为您声明了一个
Strings
的ArrayList
,但后来尝试添加一个Strings
Array
(不是String
,但是一collection
Strings
)。
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). 第二个例子是有效的,因为你声明了
Objects
ArrayList
,然后你向它添加了Objects
(理解Java中的所有东西都是一个Object
:一个String
是一个Object
,一个Array
也是例如一个Boolean
也是一个Object
,你可以尝试添加在第二个示例中为True
,您将看到它也可以工作)。
Here i see some misunderstanding of String
and String[]
both are not same, while seeing your following code 在这里,我看到一些对
String
和String[]
误解都不相同,同时看到你的下面的代码
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
. 好像你正试图添加
String[]
对象为ArrayList
类型的String
。 If you want to add name
array into list then your list type must be String[]
. 如果要将
name
数组添加到列表中,则列表类型必须为String[]
。 So becasue type are different so you cannot add String[]
object into list of type String
. 因此,因为类型不同所以你不能将
String[]
对象添加到String
类型的列表中。 But When your have list of type Object
then of course you can add any object into it. 但是当你有
Object
类型的列表时,你当然可以在其中添加任何对象。
IDE tells us about object data type clearly. IDE清楚地告诉我们对象数据类型。
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. 在第6行,名为
arraylist
对象具有类型ArrayList<java.lang.String>
但您尝试这样的事情: ArrayList<java.lang.String[]>
导致不兼容数据类型错误。
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>
. 当步骤调试到第10行时,
name1
的数据类型为java.lang.Object
, arraylist1
数据类型为ArrayList<Object>
。 Therefore, add element name1
to arraylist1
success. 因此,将元素
name1
添加到arraylist1
成功。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.