List概述
List接口是一個(gè)繼承了集合(Collection)接口的有序集合的接口蹲诀,常見的ArrayList岳守、LinkedList都以不用形式來(lái)實(shí)現(xiàn)List接口。具體說(shuō)明參考源代碼或Oracle文檔。
add()方法
add()方法是將指定的元素附加到此列表的末尾。
* Appends the specified element to the end of this list (optional
* operation).
*
* <p>Lists that support this operation may place limitations on what
* elements may be added to this list. In particular, some
* lists will refuse to add null elements, and others will impose
* restrictions on the type of elements that may be added. List
* classes should clearly specify in their documentation any restrictions
* on what elements may be added.
在列表的末尾添加元素羡忘,但是在使用是,如果添加的是自定義的對(duì)象時(shí)磕昼,卻不是直接添加該對(duì)象卷雕,而是添加該對(duì)象的引用(或者說(shuō)地址);如果添加的是String票从、int等基本類型數(shù)據(jù)時(shí)爽蝴,添加的是本身的值,而不是引用纫骑。以下測(cè)試印證。
測(cè)試
使用ArrayList測(cè)試九孩,分別添加元素為基本類型和自定義對(duì)象先馆。
基本類型
String類型
public void testStringList(){
String name = "";
List<String> list = new ArrayList<String>();
System.out.println("------------part 1------------");
System.out.println("-----name:"+name+",hashcode:"+name.hashCode());
System.out.println("-----list:"+list.toString()+",hashcode:"+list.hashCode());
name="aaa";
list.add(name);
System.out.println("------------part 2------------");
System.out.println("-----name:"+name+",hashcode:"+name.hashCode());
System.out.println("-----list:"+list.toString()+",hashcode:"+list.hashCode());
name="bbb";
list.add(name);
System.out.println("------------part 3------------");
System.out.println("-----name:"+name+",hashcode:"+name.hashCode());
System.out.println("-----list:"+list.toString()+",hashcode:"+list.hashCode());
}
打印的結(jié)果:
------------part 1------------
-----name:,hashcode:0
-----list:[],hashcode:1
------------part 2------------
-----name:aaa,hashcode:96321
-----list:[aaa],hashcode:96352
------------part 3------------
-----name:bbb,hashcode:97314
-----list:[aaa, bbb],hashcode:3084226
part 1 為初始階段,沒有賦值時(shí)name值為空躺彬,list也為空(此時(shí)list的hashcode為1煤墙,是另外一個(gè)問(wèn)題了);part 2 和 part 3 的區(qū)別是重點(diǎn)宪拥,為name賦了不同的值仿野,hashcode發(fā)生變化,list中的數(shù)據(jù)不同她君。此時(shí)list存儲(chǔ)的是基本類型元素的值脚作。
其他類型
Integer等基本類型測(cè)試也適用。
自定義類
自定義測(cè)試類:
public class BaseEntity {
private String id;
private String name;
public BaseEntity(String id){
this.id=id;
};
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "id:"+this.id+",name:"+this.name;
}
}
不同對(duì)象
測(cè)試代碼:
public void testOtherObjectList(){
List<BaseEntity> list = new ArrayList<BaseEntity>();
BaseEntity entity_1 = new BaseEntity("a1");
entity_1.setName("name-3");
list.add(entity_1);
System.out.println("----------part 1-----------");
System.out.println("---entity-----hashcode:"+entity_1.hashCode()+"--------toString:"+entity_1.toString());
System.out.println("---list-------hashcode:"+list.hashCode()+"--------toString:"+list.toString());
System.out.println(list.get(0));
BaseEntity entity_2 = new BaseEntity("a2");
entity_2.setName("name-4");
list.add(entity_2);
System.out.println("----------part 2-----------");
System.out.println("---entity-----hashcode:"+entity_2.hashCode()+"--------toString:"+entity_2.toString());
System.out.println("---list-------hashcode:"+list.hashCode()+"--------toString:"+list.toString());
System.out.println(list.get(0));
System.out.println(list.get(1));
}
打印結(jié)果:
----------part 1-----------
---entity-----hashcode:1838148248--------toString:id:a1,name:name-3
---list-------hashcode:1838148279--------toString:[id:a1,name:name-3]
id:a1,name:name-3
----------part 2-----------
---entity-----hashcode:57858394--------toString:id:a2,name:name-4
---list-------hashcode:1205880195--------toString:[id:a1,name:name-3, id:a2,name:name-4]
id:a1,name:name-3
id:a2,name:name-4
part 1是添加第一個(gè)元素,part 2是添加第二個(gè)元素球涛。兩次添加的元素劣针,類相同但卻是兩個(gè)實(shí)例對(duì)象。兩個(gè)對(duì)象的hashcode不同亿扁,賦值不同捺典,添加到list中,list的內(nèi)容也不同从祝。
同一個(gè)對(duì)象
測(cè)試代碼:
public void testObjectList(){
List<BaseEntity> list = new ArrayList<BaseEntity>();
BaseEntity entity_1 = new BaseEntity("b1");
entity_1.setName("name-1");
list.add(entity_1);
System.out.println("----------part 1-----------");
System.out.println("---entity-----hashcode:"+entity_1.hashCode()+"--------toString:"+entity_1.toString());
System.out.println("---list-------hashcode:"+list.hashCode()+"--------toString:"+list.toString());
System.out.println(list.get(0));
entity_1.setId("b2");
entity_1.setName("name-2");
list.add(entity_1);
System.out.println("----------part 2-----------");
System.out.println("---entity-----hashcode:"+entity_1.hashCode()+"--------toString:"+entity_1.toString());
System.out.println("---list-------hashcode:"+list.hashCode()+"--------toString:"+list.toString());
System.out.println(list.get(0));
System.out.println(list.get(1));
}
打印結(jié)果:
----------part 1-----------
---entity-----hashcode:88633879--------toString:id:b1,name:name-1
---list-------hashcode:88633910--------toString:[id:b1,name:name-1]
id:b1,name:name-1
----------part 2-----------
---entity-----hashcode:88633879--------toString:id:b2,name:name-2
---list-------hashcode:-1458682207--------toString:[id:b2,name:name-2, id:b2,name:name-2]
id:b2,name:name-2
id:b2,name:name-2
part 1是添加第一個(gè)元素襟己,part 2是添加第二個(gè)元素。兩次添加過(guò)程中牍陌,同一個(gè)對(duì)象兩次賦值擎浴,屬性值變了但是hashcode值沒有變,過(guò)程中對(duì)象的存儲(chǔ)沒有改變呐赡;但是list在第二次執(zhí)行add()方法后退客,兩個(gè)位置中的元素值相同。此時(shí)list中的第一個(gè)元素值改變了链嘀,對(duì)比不同對(duì)象添加的結(jié)果萌狂,可以說(shuō)明list存儲(chǔ)的是該元素對(duì)象的引用。
結(jié)論
List在執(zhí)行add()方法時(shí)怀泊,如果添加的元素是基本類型茫藏,則添加的是基本類型的值,而如果添加的是自定義的類對(duì)象時(shí)霹琼,添加的實(shí)際上是該對(duì)象的引用或者說(shuō)是虛擬地址务傲,在需要list中的某個(gè)元素時(shí),還是根據(jù)這個(gè)引用查找該對(duì)象枣申。