package collection;
public class Collection01 {
public static void main(String[] args) {
//以ArrayList演示Collection接口常用方法
//1.add
@SuppressWarnings({"all"})
List l1=new ArrayList();
l1.add("西游记");
l1.add(10);
l1.add(true);
System.out.println("l1:"+l1);
//2.remove
l1.remove(0);
l1.remove(true);
System.out.println("l1:"+l1);
//3.contains
System.out.println(l1.contains(true));
//4.size
System.out.println(l1.size());
//5.isEmpty
System.out.println(l1.isEmpty());
//6.clear
l1.clear();
System.out.println("l1:"+l1);
//7.addAll
List l2=new ArrayList();
l2.add("三国演义");
l2.add("水浒传");
System.out.println("l2:"+l2);
l1.addAll(l2);
System.out.println("l1:"+l1);
//8.removeAll
l1.removeAll(l2);
System.out.println("l1:"+l1);
//9.containsAll
System.out.println(l1.containsAll(l2));
}
}
package collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Collection12 {
public static void main(String[] args) {
Collection col=new ArrayList();
col.add(new Book("西游记","吴承恩",100));
col.add(new Book("三国演义","吴贯中",98));
col.add(new Book("红楼梦","曹雪芹",93));
System.out.println("col:"+col);
// 遍历集合
//1.先得到col对于的构造器
Iterator iterator=col.iterator();
//2.使用while循环遍历
while(iterator.hasNext()){//判断是否还有元素
//返回下一个元素,类型是Object
Object obj=iterator.next();
System.out.println("obj:"+obj);//动态绑定
}
//此时iterator执行末尾
//iterator.next() NoSuchElementException
//快捷键生成while循环遍历集合 itit+回车
//使用快捷键 ctrl+j 找到所有快捷键
//希望再次遍历
iterator=col.iterator();
//2.使用while循环遍历
while(iterator.hasNext()){//判断是否还有元素
//返回下一个元素,类型是Object
Object obj=iterator.next();
System.out.println("obj:"+obj);//动态绑定
}
}
}
class Book{
private String name;
private String author;
private double 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;
}
public Book(String name, String author, double price) {
this.name = name;
this.author = author;
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + ''' +
", author='" + author + ''' +
", price=" + price +
'}';
}
}
package collection;
import java.util.ArrayList;
import java.util.Collection;
public class Collection03 {
public static void main(String[] args) {
//Collection接口的遍历方式2--for增强循环
//增强for循环,可以代替iterator迭代器,底层就是iterator迭代器
//本质一样,只能用于遍历集合或数组
Collection col=new ArrayList();
col.add(new Book("西游记","吴承恩",100));
col.add(new Book("三国演义","吴贯中",98));
col.add(new Book("红楼梦","曹雪芹",93));
for(Object o:col ){
System.out.println("o:"+o);
}
//也可以用于数组
int[] nums={2,3,4};
for(int a:nums){
System.out.println(a);
}
}
}
package collection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Collection04 {
public static void main(String[] args) {
List l=new ArrayList();
l.add(new Dog("小黄",19));
l.add(new Dog("小黑",20));
l.add(new Dog("小蓝",21));
//迭代器
Iterator iterator=l.iterator();
while(iterator.hasNext()){
Object o=iterator.next();
System.out.println("o:"+o);
}
//增强for循环
for(Object o:l){
System.out.println("o:"+o);
}
}
}
class Dog{
private String name;
private int age;
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + ''' +
", age=" + age +
'}';
}
}
package collection;
import java.util.ArrayList;
import java.util.List;
public class Collection05 {
public static void main(String[] args) {
// List是Collection接口子接口
// 1)List集合类中的元素有序(添加和取出顺序一致),且可重复
List l=new ArrayList();
l.add("哈哈");
l.add("呵呵");
l.add("呵呵");
System.out.println("l:"+l);
l.add("呵呵");
System.out.println("l:"+l);
//2.List集合中的每个元素都有其对应的顺序索引,即支持索引
//3.List容器中的元素都对应一个整数型的序号记载其在容器中的位置,可以根据序号存取容器中的元素
System.out.println(l.get(3));
//4.List接口的实现类很多 常用的三个ArrayList,linkedList,Vector
//List接口常用方法
//1.void add(int index,Object else);在index位置插入else元素
l.add(0,"你好");//没有下标,默认在末尾
//2.boolean addAll(int index,Collection elses);在index位置开始将elses所有元素插入
List l2=new ArrayList();
l.addAll(1,l2);//没有下标,默认在末尾
//3. Object get(int index)//获取指定index位置的元素
System.out.println(l.get(0));//查
//4.int indexOf(Object obj)//返回ojb在集合中首次出现的位置
System.out.println(l.indexOf("呵呵"));
//5.int lastIndexOf(Object obj);返回obj在当前集合中末次出现的位置
System.out.println(l.indexOf("呵呵"));
//6.Object remove(int index);删除指定位置元素,并返回此元素
l.remove(0);
//7.Object set(int index,Object ele);设置指定位置元素为ele,相当于替换
l.set(0,"你好");
//8.List subList(int fromIndex,toIndex);返回指定范围的集合
List returnList=l.subList(2,3);//(fromIndex,toIndex]
System.out.println("returnList:"+returnList);
}
}
package collection;
import java.util.ArrayList;
import java.util.List;
public class Collection06 {
public static void main(String[] args) {
List l=new ArrayList();
l.add("你");
l.add("是");
l.add("最棒");
l.add("的");
l.add("请相信自己");
l.add("加油");
l.add("相信自己");
l.add("奥里给");
l.add("奥里给");
l.add("奥里给");
l.add("奥里给");
l.add(2,"韩顺平教育");
l.get(4);
l.remove(5);
for(Object o:l){
System.out.println(o);
}
}
}
package collection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Collection07 {
public static void main(String[] args) {
List l=new ArrayList();
l.add("jack");
l.add("helo");
l.add("sdf");
//1.迭代器
Iterator iterator=l.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
System.out.println("next:"+next);
}
//2.增强for
for(Object o:l){
System.out.println("o:"+o);
}
//3.普通for循环
for(int i=0;i
package collection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Collection08 {
public static void main(String[] args) {
List l=new ArrayList();
l.add(new Book1("三国演义",200,"罗贯中"));
l.add(new Book1("水浒传",190,"施耐庵"));
l.add(new Book1("西游记",210,"吴承恩"));
sort(l);
Iterator iterator=l.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
System.out.println(next);
}
}
public static void sort(List l){
int listSize=l.size();
for(int i=0;ibook2.getPrice()){
l.set(j,book2);
l.set(j+1,book1);
}
}
}
}
}
class Book1{
private String name;
private double price;
private String author;
public Book1(String name, double price, String author) {
this.name = name;
this.price = price;
this.author = author;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "名称:"+name+"t"+"价格:"+price+"t"+author;
}
}



