方法名 | 说明 |
---|---|
public boolean add(E e) | 添加元素,添加成功返回true |
public void clear() | 清空集合的元素 |
public boolean isEmpty() | 判断集合是否为空,为空返回true,反之 |
public int size() | 获取集合的大小 |
public boolean contains(Obijec obj) | 判断集合是否包含某个元素 |
public boolean remove(E e) | 删除某个元素:如果有多个重复元素默认删除前面的第一个 |
public Object[] toArray() | 把集合转换成数组 |
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
public class Test {
public static void main(String[] args) {
Collection<String> c = new ArrayList<>(); // 多态写法
// public boolean add(E e) 添加元素,添加成功返回true
c.add("java1");
c.add("java2");
c.add("java2");
c.add("java3");
System.out.println(c);
// public void clear() 清空集合的元素
c.clear();
System.out.println(c);
// public boolean isEmpty() 判断集合是否为空,为空返回true,反之
System.out.println(c.isEmpty());
// public int size() 获取集合的大小
System.out.println(c.size());
// public boolean contains(Obijec obj) 判断集合是否包含某个元素
System.out.println(c.contains("java1"));
// public boolean remove(E e) 删除某个元素:如果有多个重复元素默认删除前面的第一个
c.remove("java1");
System.out.println(c);
// public Object[] toArray() 把集合转换成数组
Object[] arr = c.toArray();
System.out.println(Arrays.toString(arr));
System.out.println("----------------------------");
// 把一个集合中的全部数据倒入到另一个集合中去
Collection<String> c1 = new ArrayList<>();
c1.add("java1");
c1.add("java2");
Collection<String> c2 = new ArrayList<>();
c2.add("java3");
c2.add("java4");
c1.addAll(c2); //就是把c2中的数据全部倒入到c1中去,c2的内容不变
System.out.println(c1);
System.out.println(c2);
}
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Test {
public static void main(String[] args) {
Collection<String> c = new ArrayList<>();
c.add("张三");
c.add("李四");
c.add("王五");
c.add("赵六");
Iterator<String> it = c.iterator();
while (it.hasNext()){
String ele = it.next();
System.out.println(ele);
}
}
}
?
import java.util.ArrayList;
import java.util.Collection;
public class Test {
public static void main(String[] args) {
Collection<String> c = new ArrayList<>();
c.add("张三");
c.add("李四");
c.add("王五");
c.add("赵六");
String[] arr = {"阿瓦","阿萨","斯特"};
for (String ele : c){
System.out.println(ele);
}
for (String ele2 : arr){
System.out.println(ele2);
}
}
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Consumer;
public class Test {
public static void main(String[] args) {
Collection<String> c = new ArrayList<>();
c.add("张三");
c.add("李四");
c.add("王五");
c.add("赵六");
// c.forEach(new Consumer<String>() {
// @Override
// public void accept(String s) {
// System.out.println(s);
// }
// });
//
// c.forEach((String s) -> {
// System.out.println(s);
// });
//
// c.forEach( s -> System.out.println(s));
c.forEach(System.out::println);
}
}
Movie类
public class Movie {
private String name;
private double score;
private String actor;
public Movie() {
}
public Movie(String name, double score, String actor) {
this.name = name;
this.score = score;
this.actor = actor;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public String getActor() {
return actor;
}
public void setActor(String actor) {
this.actor = actor;
}
}
Test类
import java.util.ArrayList;
import java.util.Collection;
public class Test {
public static void main(String[] args) {
Collection<Movie> movies = new ArrayList<>();
movies.add(new Movie("阿甘正传",9.5,"汤姆·汉克斯"));
movies.add(new Movie("霸王别姬",9.6,"张国荣、张丰毅"));
movies.add(new Movie("肖申克的救赎",9.7,"罗宾斯"));
for (Movie movie : movies) {
System.out.println("电影名:" + movie.getName());
System.out.println("评分:" + movie.getScore());
System.out.println("主演:" + movie.getActor());
System.out.println("-------------------------------");
}
}
}