List 集合基本概述
序列集合LIst ,此接口可以对列表的每个元素插入位置精确的控制,用户可以根据元素的整数索引访问元素并遍历搜索整个列表中的元素
(图片源自网络)
List 基本功能
add(E e) 向列表的尾部添加指定的元素(可选操作)。
add(int index, E element) 在列表的指定位置插入指定元素(可选操作)。
clear() 从列表中移除所有元素(可选操作)。
addAll(Collection<? extends E> c) 添加指定 collection 中的所有元素到此列表的结尾,顺序是指定 collection 的迭代器返回这些元素的顺序(可选操作)。
contains(Object o) 如果列表包含指定的元素,则返回 true。
get(int index) 返回列表中指定位置的元素
isEmpty() 如果列表不包含元素,则返回 true。
iterator() 返回按适当顺序在列表的元素上进行迭代的迭代器。
remove(int index) 移除列表中指定位置的元素(可选操作)。
remove(Object o) 从此列表中移除第一次出现的指定元素(如果存在)(可选操作)。
set(int index, E element) 用指定元素替换列表中指定位置的元素(可选操作)。
size() 返回列表中的元素个数
toArray() 返回按适当顺序包含列表中的所有元素的数组(从第一个元素到最后一个元素)。
List用例一:
对字符串简单操作并遍历
package com.hs.ArrayListText;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
// 字符串操作
public class ArrayListText01 {
public static void main(String[] args) {
Collection c = new ArrayList(); //cllection 是个接口
ArrayList a = new ArrayList();
c.add("hellow");
c.add("world");
a.add("hellow"); // 在直接add加入一个字符串时, 隐形 创建了一个Object类 (Object obj = "hellow")
a.add("JAVA");
System.out.println(c); //打印C集合的元素
c.remove("hellow"); //移除元素“hellow”
a.addAll(c); //添加一个集合的元素
// a.removeAll(c); //移除一个集合的元素
System.out.println(c); //打印C集合的元素
System.out.println(a); //打印a集合的元素
System.out.println();
Object[] o =a.toArray(); //将ArrayList集合转换为 数组类
for (Object object : o) {
String s = (String) object; //若想打印数组元素的长度, 需转换成字符串类型
System.out.println(s+"-----"+s.length());
}
System.out.println();
System.out.println();
Iterator it = a.iterator();
while(it.hasNext()){
String s = (String)it.next();
System.out.println(s+"-----"+s.length());
}
}
}
用例二:
自定义对象存储并遍历
自定义类
package com.hs.ArrayListText;
public class Student {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
super();
this.name = name;
this.age = age;
}
}
操作部分
package com.hs.ArrayListText;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
//自定义对象
public class ArrayListTest02 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Collection c = new ArrayList();
//List list = new ArrayList();
Student s1 = new Student("张三",18);
Student s2 = new Student("李四",19);
Student s3 = new Student("王麻子",20);
c.add(s1);
c.add(s2);
c.add(s3);
// ArrayList的遍历
Object obj[] = c.toArray();
for (Object object : obj) {
Student s = (Student)object;
System.out.println(s.getName()+"--------"+s.getAge());
}
System.out.println();
System.out.println();
System.out.println();
Iterator it = c.iterator();
while(it.hasNext()){ // boolean hasNext() 的作用是判断是否还有下一个元素
Student s = (Student)it.next(); // 每使用一次 it.next(), 就访问一次对象,自增加一
System.out.println(s.getName()+"--------"+s.getAge());
}
// for循环写法的好处: 循环结束后自动释放 it
// for(Iterator it = c.iterator();it.hasNext();){
// Student s = (Student)it.next();
// System.out.println(s.getName()+"--------"+s.getAge());
// }
}
}