public interface Collection<E> extends lterable<e>
import java.util.ArrayList;
import java.util.List;
public class CollectionMethod {
public static void main(String[] args) {
List list = new ArrayList();
// add 添加单个元素
list.add("jack");
list.add(10);
list.add(true);
System.out.println("list="+list);
//remove:删除指定元素
list.remove(0);
System.out.println("list="+list);
//contanins:查找元素是否存在
System.out.println(list.contains("jack"));
//size:获取元素个数
System.out.println(list.size());
//isEmpty:判断是否为空
System.out.println(list.isEmpty());
//clear:清空
//list.clear()
//addAll:添加多个元素
ArrayList list2 = new ArrayList();
list2.add("hhh");
list2.add("hhh");
list.addAll(list2);
System.out.println(list);
//containsAll:查找多个元素是否都存在
System.out.println(list.containsAll(list2));
//removeAll:删除多个元素
list.add("聊斋");
list.removeAll(list2);
System.out.println(list);
}
}
快捷键itit
package top.ltyzqhh.collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class CollectionIterator {
public static void main(String[] args) {
Collection col = new ArrayList();
col.add(new Book("三国演义","罗贯中",10.1));
col.add(new Book("西游记","吴承恩",10.3));
col.add(new Book("红楼梦","曹雪芹",10.5));
//1.先得到col对于的迭代器
Iterator iterator = col.iterator();
//2while循环遍历
while (iterator.hasNext()) {//判断是否还有数据
//返回下一个元素,类型是Object
Object next = iterator.next();
System.out.println("obj="+next);
}
//3.当退出while循环后,Iterator指向最后的元素,
//4.如果希望再次遍历。需要重置我们的迭代器
iterator=col.iterator();
}
}
class Book{
private String name;
private String author;
private double price;
public Book() {
}
public Book(String name, String author, double price) {
this.name = name;
this.author = author;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\\'' +
", author='" + author + '\\'' +
", price=" + price +
'}';
}
}
增强for循环也可用于数组,底层依然是迭代器 快捷键I
package top.ltyzqhh.collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class CollectionIterator {
public static void main(String[] args) {
Collection col = new ArrayList();
col.add(new Book("三国演义","罗贯中",10.1));
col.add(new Book("西游记","吴承恩",10.3));
col.add(new Book("红楼梦","曹雪芹",10.5));
//增强for循环
//底层依然是迭代器
//可以理解成简化版的迭代器
//快捷键 I
for (Object book:col) {
System.out.println(book);
}
}
}
class Book{
private String name;
private String author;
private double price;
public Book() {
}
public Book(String name, String author, double price) {
this.name = name;
this.author = author;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\\'' +
", author='" + author + '\\'' +
", price=" + price +
'}';
}
}