package com.ethjava;
import java.util.*;
public class Arraylistlianxi {
public static void main(String args[]) {
// 创建一个空的数组链表对象list,list用来存放String类型的数据
ArrayList<String> list = new ArrayList<String>();
ArrayList<Integer> list2=new ArrayList<>(7);//如果你需要创建一个指定初始容量的数组链表,你可以这样做
list2.add(1);
// 增加元素到list对象中
list.add("Item1");//位置0
list.add("Item2");//位置1
list.add(2, "Item3"); // 位置2,此条语句将会把“Item3”字符串增加到list的第3个位置。
list.add("Item4");//位置3
list.add(2,"yxf");//将位置2的item3挤走,放上yxf,原来的内容往后移
list.add("Item2");
// 显示数组链表中的内容
System.out.println("The arraylist contains the following elements: "
+ list);
//打印结果:The arraylist contains the following elements: [Item1, Item2, yxf, Item3, Item4]
//The arraylist contains the following elements: [Item1, Item2, yxf, Item3, Item4, item2]
// 检查元素的位置
int pos = list.indexOf("Item2");
System.out.println("The index of Item2 is: " + pos);//The index of Item2 is: 1
//只能检查到第一个Item2的位置 1
int pos2=list.lastIndexOf("Item2");
System.out.println("the last appear position: "+pos2);
//int lastIndexOf(Object o);返回元素在链表中最后一次出现的位置,如果返回-1,表示链表中没有这个元素
//the last appear position: 5
// 检查数组链表是否为空
boolean check = list.isEmpty();
System.out.println("Checking if the arraylist is empty: " + check);//list不为空,打印false
boolean check2=list2.isEmpty();
System.out.println(check2);//是空的,就打印true,
//Checking if the arraylist is empty: false
//true
// 获取链表的大小
int size = list.size();
System.out.println("The size of the list is: " + size);
int size2 = list2.size();
System.out.println(size2);//空的list,就打印0
//The size of the list is: 6
//0
// 检查数组链表中是否包含某元素
boolean element = list.contains("Item5");//不包含
System.out.println("Checking if the arraylist contains the object Item5: "
+ element);//Checking if the arraylist contains the object Item5: false
boolean yxfelement = list.contains("yxf");
System.out.println(yxfelement);//包含就打印true
// 获取指定位置上的元素
String item = list.get(0);
System.out.println("The item is the index 0 is: " + item);
Integer item2 = list2.get(0);
System.out.println(item2);//输出1
//这里注意返回的数据类型,要与创建时相同。。。
// 遍历arraylist中的元素
// 第1种方法: 循环使用元素的索引和链表的大小
System.out.println("Retrieving items with loop using index and size list");
for (int i = 0; i < list.size(); i++) {
System.out.println("Index: " + i + " - Item: " + list.get(i));
}
//Retrieving items with loop using index and size list
//Index: 0 - Item: Item1
//Index: 1 - Item: Item2
//Index: 2 - Item: yxf
//Index: 3 - Item: Item3
//Index: 4 - Item: Item4
//Index: 5 - Item: item2
// 第2种方法:使用foreach循环
System.out.println("Retrieving items using foreach loop");
for (String str : list) {
System.out.println("Item is: " + str);
}
//etrieving items using foreach loop
//Item is: Item1
//Item is: Item2
//Item is: yxf
//Item is: Item3
//Item is: Item4
//Item is: item2
// 第三种方法:使用迭代器
// hasNext(): 返回true表示链表链表中还有元素
// next(): 返回下一个元素
System.out.println("Retrieving items using iterator");
for (Iterator<String> it = list.iterator(); it.hasNext();) {
System.out.println("Item is: " + it.next());
}
//Retrieving items using iterator
//Item is: Item1
//Item is: Item2
//Item is: yxf
//Item is: Item3
//Item is: Item4
//Item is: item2
for(Iterator<Integer> it=list2.iterator();it.hasNext();){
System.out.println("Item is : "+it.next());
}
//Item is : 1
// 替换元素
list.set(1, "NewItem");
System.out.println("The arraylist after the replacement is: " + list);
//The arraylist after the replacement is: [Item1, NewItem, yxf, Item3, Item4, item2]
// 移除元素
// 移除第0个位置上的元素
list.remove(0);
System.out.println("The arraylist after the replacement is: " + list);
//The arraylist after the replacement is: [NewItem, yxf, Item3, Item4, item2]
// 移除第一次找到的 "Item3"元素
list.remove("Item3");//后面加某个元素
System.out.println("The final contents of the arraylist are: " + list);
//The final contents of the arraylist are: [NewItem, yxf, Item4, item2]
list.remove(0);//删除第0位置的元素
System.out.println("The final contents of the arraylist are: " + list);
//The final contents of the arraylist are: [yxf, Item4, item2]
// 转换 ArrayList 为 Array
String[] simpleArray = list.toArray(new String[list.size()]);
//list.toArray获取一个数组,数组中所有元素是链表中的元素.(即将链表转换为一个数组)
System.out.println("The array created after the conversion of our arraylist is: "
+ Arrays.toString(simpleArray));
//he array created after the conversion of our arraylist is: [yxf, Item4, item2]
System.out.println("The array created after the conversion of our arraylist is: "
+simpleArray);
//The array created after the conversion of our arraylist is: [Ljava.lang.String;@28a418fc
//直接打印数组只会输出数组的地址
}
}
c参考:https://www.cnblogs.com/ShallByeBye/p/8419027.html