java集合框架

在这里插入图片描述
import java.util.ArrayList;
import java.util.Collection;

/*

  • java.util.Collection接口
  •  所有单列集合的最顶层的接口,里边定义了所有单列集合共性的方法
    
  •  任意的单列集合都可以使用Collection接口中的构造方法
    
  •  共性的方法:
    
  •  	public boolean add(E e); 把给定的对象添加到当前的集合当中。
    
  •  	public void clear();	清空集合中的所有元素。 
    
  •  	public boolean remove(E e);	把给定的对象在当前集合中删除 
    
  •  	public boolean contains(E e); 判断当前集合中是否包含给定的对象。
    
  •  	public boolean isEmpty(); 判断当前集合是否为空。 
    
  •  	public int size(); 返回集合中元素的个数。 
    
  •  	public Object[] toArray(); 把集合中的元素,存储到数组当中。 
    

*/

public class Collection练习 {
	public static void main(String[] args) {
		Collection<String> coll = new ArrayList<>();
		System.out.println(coll);
		
		//public boolean add(E e); 把给定的对象添加到当前的集合当中。
		coll.add("SF");
		coll.add("SDF");
		coll.add("GE");
		coll.add("FER");
		System.out.println(coll);
		
		//public boolean remove(E e);	把给定的对象在当前集合中删除
		coll.remove("CW");
		System.out.println(coll);
		
		//public int size(); 返回集合中元素的个数。 
		System.out.println(coll.size());
		
		//public boolean isEmpty(); 判断当前集合是否为空。
		boolean b1 = coll.isEmpty();
		System.out.println(b1);
		
		//public boolean contains(E e); 判断当前集合中是否包含给定的对象。
		boolean b2 = coll.contains("ERG");
		System.out.println(b2);
		
		//public Object[] toArray(); 把集合中的元素,存储到数组当中。
		Object[] obj = coll.toArray();
		for(int i = 0; i < obj.length;i++)
			System.out.println(obj[i]);
	}
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

//迭代器的作用就是遍历集合
public class 迭代器 {
	public static void main(String[] args) {
		Collection<String> coll = new ArrayList<>();
		coll.add("WER");
		coll.add("ERF");
		coll.add("讲道理");
		coll.add("讲缘分");
		coll.add("WE");
		
		/*
		 * 使用集合中的方法iterator()获取迭代器的实现类对象,使用Itretor接口接收(多态)
		 * 		注意:
		 * 			Iterator<E>接口也是有范型的,迭代器的范型跟着集合走,集合是什么范型,迭代器就是什么范型
		 */
		Iterator<String> it = coll.iterator();
		
		String s;
		//.hasNext()判断是否还有下一个元素,返回布尔类型
		while(it.hasNext()) {
			//.Next()输出下一个元素
			s = it.next();
			System.out.println(s);
		}
	}
}

import java.util.LinkedList;

/*

  • LinkedList集合的特点:
  • 1.底层是一个链表结构:查询快,增删快
  • 2.里面包含了大量操作首位元素的方法
  • 注意:使用LinkedList集合特有的方法,不能使用多态
  • public void addFirst()将指定元素插入此列表的开头
  • public void addLast()将指定元素添加到此列表的结尾。
  • public void push()将元素推入此列表所表示的堆栈
  • public E getFirst()返回此列表的第一个元素
  • public E getLast()返回此列表的第一个元素
  • public E removeFirst()返回此列表的第一个元素
  • public E removeLast()返回此列表的第一个元素
  • public E pop()返回此列表的第一个元素
    */
public class LinkedList接口 {
	public static void main(String[] args) {
		LinkedList<String> list = new LinkedList<>();
		
		list.add("d");
		list.add("gf");
		list.add("v");
		list.add("f");
		list.add("er");
		list.add("g");
		list.add("h");
		System.out.println(list);
		
		
		//public void addFirst()将指定元素插入此列表的开头
		list.addFirst("23");
		System.out.println(list);
		//public void push()将元素推入此列表所表示的堆栈
		list.push("23");
		System.out.println(list);
		//public void addLast()将指定元素添加到此列表的结尾。
		list.addLast("52");
		System.out.println(list);
		
		//public E getFirst()返回此列表的第一个元素
		System.out.println(list.getFirst());
		//public E getLast()返回此列表的第一个元素
		System.out.println(list.getLast());
		
		//public E removeFirst()返回此列表的第一个元素
		list.removeFirst();
		System.out.println(list);
		//public E removeLast()返回此列表的第一个元素
		list.removeLast();
		System.out.println(list);
		//public E pop()返回此列表的第一个元素
		list.pop();
		System.out.println(list);
		
		list.clear();
		System.out.println(list);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值