集合、Collection、泛型相关

Collection接口

2.1 Collection中常用的方法
方法名描述
add(E e)确保此 collection 包含指定的元素(可选操作)。
addAll(Collection<? extends E> c)将指定 collection 中的所有元素都添加到此 collection 中(可选操作)。
clear()移除此 collection 中的所有元素(可选操作)。
contains(Object o)如果此 collection 包含指定的元素,则返回true。
containsAll(Collection<?> c)如果此 collection 包含指定 collection 中的所有元素,则返回 true。
equals(Object o)比较此 collection 与指定对象是否相等。
isEmpty()如果此 collection 不包含元素,则返回true。
iterator()返回在此 collection 的元素上进行迭代的迭代器。
remove(Object o)从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。
removeAll(Collection<?> c)移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。
retainAll(Collection<?> c)仅保留此 collection 中那些也包含在指定 collection 的元素(可选操作)。
size()返回此 collection 中的元素数。
toArray()返回包含此 collection 中所有元素的数组。

上机练习1

		//1创建Collection对象
		Collection collection=new ArrayList();
		//2操作
		//2.1添加
		collection.add("张三");
		collection.add("李四");
		collection.add("他二大爷");
		collection.add("他老爷");
		collection.add("七大妈");
		collection.add("张三");
		System.out.println("集合的元素个数:"+collection.size());
		//2.2删除
		//2.2.1删除一个
//		collection.remove("张三");
//		System.out.println("删除之后的数据个数:"+collection.size());
		//2.2.2清空
//		collection.clear();
//		System.out.println("清空之后:"+collection.size());
		
		
		//4判断
		//4.1判断是否存在
		boolean b=collection.contains("王五");
		boolean b2=collection.contains("他老爷");
		System.out.println(b);
		System.out.println(b2);
		//4.2判断是否为空
		System.out.println(collection.isEmpty());
	
		//5查找
		
		//6其他方法
		Collection c1=new ArrayList();
		c1.add("中国");
		c1.add("朝鲜");
		Collection c2=new ArrayList();
		c2.add("中国");
		c2.add("朝鲜");
		System.out.println("c1.equals(c2):"+c1.equals(c2));
		System.out.println("c1==c2:"+(c1==c2));

上机练习2

		Collection collection=new ArrayList();
		//1添加
		collection.add(111);//111自动装箱 new Integer(111)
		collection.add(222);
		collection.add("111");
		collection.add("222");
		//2删除
		System.out.println("元素的个数:"+collection.size());
		System.out.println("删除之前:"+collection);
		collection.remove("111");
		System.out.println("删除之后:"+collection.size());
		System.out.println("删除之后:"+collection);
		
		
第三节 List接口

在Collection接口的基础上扩展了下标这样的功能

方法名描述
add(int index, E element)在列表的指定位置插入指定元素(可选操作)。
addAll(int index, Collection<? extends E> c)将指定 collection 中的所有元素都插入到列表中的指定位置(可选操作)。
containsAll(Collection<?> c)如果列表包含指定 collection 的所有元素,则返回true。
get(int index)返回列表中指定位置的元素。
indexOf(Object o)返回此列表中第一次出现的指定元素的索引;如果此列表不包含该元素,则返回 -1。
lastIndexOf(Object o)返回此列表中最后出现的指定元素的索引;如果列表不包含此元素,则返回 -1。
listIterator()返回此列表元素的列表迭代器(按适当顺序)。
remove(int index)移除列表中指定位置的元素(可选操作)。
set(int index, E element)用指定元素替换列表中指定位置的元素(可选操作)。
subList(int fromIndex, int toIndex)返回列表中指定的 fromIndex(包括 )和 toIndex(不包括)之间的部分视图。
  • 注意
//remove(int index) 和 remove(Object o)
		//当出现删除数字时,优先删除下标


ArrayList list=new ArrayList();
		list.add(1);
		//将值增加的到指定位置
		list.add(2);
		
		list.remove(1);//将2删除
3.1 List接口的存储特点
1 相对有序存储,可以存储相同元素(不排重),可以通过下标访问集合元素

3.2 List接口的实现类
3.2.1 ArrayList类
ArrayList类是List接口的大小可变数组的实现。实现了所有可选列表操作,并允许包括null在内的所有元素。

存储特点:
	相对有序存储,可以存储相同元素(不排重),可以通过下标访问集合元素,通过数组实现的集合
存储结构:数组
  • 注意
ArrayList的初始化容量是多大;
//在JDK1.7之前,他会初始化一个10个长度的数组

不是,当发生new ArrayList时,ArrayList的底层提供了一个空数组
当第一次调用add方法时,那么会对数组进行扩容,扩容成一个容量为10的数组,并且将形参的值添加到第0个下标
他的扩容规则
int Capacity=10;
Capacity=Capacity+Capacity/2;
10,15,22,33,49
  • 增强型for循环
语法
//遍历的数据类型必须为引用数据类型
for(容器中的引用数据类型 变量名:容器名)//变量名就是每一个对象package listDemo;

import java.util.ArrayList;
import java.util.List;



/**
 * @author Eric 15665627080@163.com
 * @date 2019年8月13日
 * @time 上午10:11:11
 * package listDemo;
 * public class ListDemo{ }
 *  相对有序存储,
 *  可以存储相同元素(不排重),
 *  可以通过下标访问集合元素
 */
public class ListDemo {
	
	public static void main(String[] args) {
		
		
		
	
		ArrayList<DVD> list=new ArrayList<DVD>();
		
		list.add(new DVD("CD0001", "满城尽带黄金甲", "烂片"));
		list.add(new DVD("CD0002", "哪吒", "动漫"));
		list.add(new DVD("CD0003", "战狼", "主流片"));
		list.add(new DVD("CD0005", "倩女幽魂", "鬼片"));
		list.add(new DVD("CD0006", "2012", "灾难片"));
		
		//小for循环
		/*for (int i = 0; i <list.size(); i++) {
			System.out.println(list.get(i).toString());
		}*/
		//增强型for循环,适用于数组,集合
		for(DVD d:list){
			System.out.println(d.toString());
			
		}
		
		String[] ss={"123","456"};
		for(String s:ss){
			System.out.println(s);
		}
		
	}

}

代码实现:

public class ArrayListDemo {
         	public static void main(String[] args) {
              	//创建一个ArrayList集合
            	ArrayList<String> list = new ArrayList<>();//构造方法中的泛型可以省略
              	list.add("zhangsan");//向集合中添加元素
              	list.add("lisi");
              	list.add("wangwu");
              	System.out.println(list.isEmpty());//判断list集合是否为空集合
              	System.out.println(list.size());//查看集合中元素的个数
              	System.out.println(list.get(1));//获取集合中下标为1的元素
              	System.out.println(list.set(1,"zhaoliu"));//修改集合中下标为1的元素
              	System.out.println(list.contains("wangwu"));//查看"wangwu"是否是集合中的元素
              	list.remove("wangwu");//删除集合中"wangwu"元素
              	list.remove(1);//删除集合中下标为1的元素
              	//for循环遍历集合
                for(int i=0; i<list.size(); i++){
                  System.out.println(list.get(i));
                }
              	
              
         }
}

案例

package listDemo;

import java.lang.management.ManagementFactory;
import java.util.ArrayList;

/**
 * @author Eric 15665627080@163.com
 * @date 2019年8月13日
 * @time 下午2:20:47 package listDemo; public class Test{ } 类说明
 */
public class Test {

	public static void main(String[] args) {
		ArrayList<Student> list = new ArrayList<Student>();
		list.add(new Student("张三", '男', 18, 92));
		list.add(new Student("张三1", '男', 18, 86));
		list.add(new Student("张三2", '女', 18, 73));
		list.add(new Student("张三3", '女', 18, 92));

		list.add(new Student("张三4", '女', 18, 92));
		list.add(new Student("张三5", '女', 18, 36));
		list.add(new Student("张三6", '男', 18, 92));
		list.add(new Student("张三7", '男', 18, 30));
		list.add(new Student("张三8", '女', 18, 63));

		list.add(new Student("张三9", '男', 18, 92));
		list.add(new Student("张三10", '男', 18, 98));
		
		
		int manNum=0;//男生人数
		int count=0;
		for (int i = 0; i < list.size(); i++) {
			count+=list.get(i).getGread();
			if(list.get(i).getSex()=='男'){
				manNum++;
				
			}
		}
		System.out.println("男生人数:"+manNum);
		System.out.println("女生人数:"+(list.size()-manNum));
	
		//求平均成绩
		System.out.println((double)count/list.size());
	
	}

}

  • Comparable接口
package listDemo;
/**
 * @author Eric 15665627080@163.com
 * @date 2019年8月13日
 * @time 下午2:18:20
 * package listDemo;
 * public class Studet{ }
 * 类说明	
 */
public class Student implements Comparable<Student>{
	private String name;
	private char sex;
	private int age;
	private int gread;//成绩
	public Student(String name, char sex, int age, int gread) {
		super();
		this.name = name;
		this.sex = sex;
		this.age = age;
		this.gread = gread;
	}
	public Student() {
		super();
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public char getSex() {
		return sex;
	}
	public void setSex(char sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public int getGread() {
		return gread;
	}
	public void setGread(int gread) {
		this.gread = gread;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", sex=" + sex + ", age=" + age + ", gread=" + gread + "]";
	}
	@Override
	public int compareTo(Student o) {
		if(this.gread==o.getGread()){
			return 0;
		}else if(this.gread>o.getGread()){
			return 1;
		}else{
			return -1;
		}
	}
	
	

}





package listDemo;

import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.Collections;

/**
 * @author Eric 15665627080@163.com
 * @date 2019年8月13日
 * @time 下午2:20:47 package listDemo; public class Test{ } 类说明
 */
public class Test {

	public static void main(String[] args) {
		ArrayList<Student> list = new ArrayList<Student>();
		list.add(new Student("张三", '男', 18, 92));
		list.add(new Student("张三1", '男', 18, 86));
		list.add(new Student("张三2", '女', 18, 73));
		list.add(new Student("张三3", '女', 18, 92));

		list.add(new Student("张三4", '女', 18, 92));
		list.add(new Student("张三5", '女', 18, 36));
		list.add(new Student("张三6", '男', 18, 92));
		list.add(new Student("张三7", '男', 18, 30));
		list.add(new Student("张三8", '女', 18, 63));

		list.add(new Student("张三9", '男', 18, 92));
		list.add(new Student("张三10", '男', 18, 98));
	
		for(Student s:list){
			System.out.println(s.toString());
		}
		
		
		
		
		Collections.sort(list);
		
		System.out.println("***************************");
		for(Student s:list){
			System.out.println(s.toString());
		}
	}

}




  • 总结
如果想使用Collections.sort()的方法对集合进行排序
1.首先泛型类型的javaBean必须实现Comparable接口,并且将T改成该Bean类型
2.重写compareTo方法,重设对象之间排序条件,此时用属性进行对比,返回正数,0,负数即可(具体升序降序,看你返回的数)
3.如果对对象数组也想使用Arrays.sort(),同理
3.2.2 LinkedList类
LinkedList类是List接口的链接列表实现。实现所有可选的列表操作,并且允许所有元素(包括null)。
存储特点:
	相对有序存储,可以存储相同元素(不排重),可以通过下标访问集合元素,通过链表实现的集合
存储结构:双向链表
总结一句话,ArrayList和LinkedList在使用上一模一样
**
 * LinkedList的使用
 * 存储结构:链表
 * @author wgy
 *
 */
public class Demo5 {
	public static void main(String[] args) {
		//创建集合
		LinkedList<String> linkedList=new LinkedList<>();
		//1添加元素
		linkedList.add("苹果");
		linkedList.add("葡萄");
		linkedList.add("西瓜");
		linkedList.add("芒果");
		linkedList.add("桃子");
	
		System.out.println("元素个数:"+linkedList.size());
		System.out.println("打印:"+linkedList.toString());
		//2删除
//		linkedList.remove(0);
//		System.out.println("删除之后:"+linkedList.toString());
		//3遍历
		//3.1for
		//3.2foreach
	
		
		
		//4判断
		System.out.println("isEmpty:"+linkedList.isEmpty());
		System.out.println("苹果:"+linkedList.contains("苹果"));
		//5查找位置
		System.out.println("桃子的位置:"+linkedList.indexOf("桃子"));
		
	}
}

ArrayList和LinkedList的总结

ArrayList存储结构是数组,LinkedLis存储结构是链表。

ArrayList集合适用在对元素查询、修改操作,不适合插入和删除。
LinkedList集合适用在对元素插入和删除操作,不适合遍历和查找。
总结
1 集合概念,用来存储一组数据的容器。和数组类似
	数组是长度固定的,集合长度可以变化。
	数组能存储基本类型和引用类型,集合只能存储引用类型。
2 Collection接口,父接口, add()  remove()  clear()  contains()  iterator()
3 Collection有两个子接口 List和Set

6 List接口 
     List接口的特点:有序的,可以重复
7 ArrayList实现类
     存储结构:数组
     适合查找和修改
  重点讲解了:
  1.ArrayList初始化容量以及他的动态扩容
  2,ArrayList的crud
  3.list如何进行排序:javaBean实现compareabale接口,重写compareTo方法
8 LinkedList实现
     存储结构:双向链表
     适合做添加,删除
     
9.面试高频考点:
ArrayList和linkedList之间区别
   
   


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值