创建list集合_集合框架(二)——List(列表)相关及泛型

715ea6c70020cd5f9fe72878dcec12cc.png

ArrayList和Linkedlist的练习

练习一:

package list;

import java.util.ArrayList;
import java.util.Iterator;

public class Demo1_ArrayList {

	/*
	 *  A:案例演示
	 * 需求:ArrayList去除集合中字符串的重复值(字符串的内容相同)
	 * 思路:创建新集合方式
	 * 1.创建静态方法,明确返回值为ArrayLi类型,明确参数列表
	 * 2.创建新的集合并获取迭代器
	 * 3.去除重复元素并返回新的集合
	 */
	public static void main(String[] args) {
		ArrayList list=new ArrayList();
		list.add("a");
		list.add("a");
		list.add("b");
		list.add("b");
		list.add("b");
		list.add("c");
		list.add("c");
		list.add("c");
		list.add("c");
		
		ArrayList newList=getSingle(list);
		System.out.println(newList);
	}
	public static ArrayList getSingle(ArrayList list){
		ArrayList newList=new ArrayList();	//创建新集合
		Iterator it=list.iterator();		//获取迭代器
		while(it.hasNext()){				
			if(!newList.contains(it.next()))
				newList.add(it.next());		//判断重复,不重复则添加
		}
		return newList;
		
	}
	
}

f35a35a2f7075c8793d27e4382175919.png

练习二:

该练习关键在于重写Student类的equals方法

类Demo_2ArrayList

package list;

import java.util.ArrayList;
import java.util.Iterator;

import demo_bean.Student;

public class Demo_2ArrayList {

	/*
	 *  A:案例演示
	 * 需求:ArrayList去除集合中自定义对象的的重复值(同姓名同年龄表示同一个人)
	 * 思路:创建新集合方式
	 * 1.创建静态方法,明确返回值为ArrayLi类型,明确参数列表
	 * 2.创建新的集合并获取迭代器
	 * 3.去除重复元素并返回新的集合
	 */
	public static void main(String[] args) {
		ArrayList list=new ArrayList();
		list.add(new Student("张三", 23));
		list.add(new Student("张三", 23));
		list.add(new Student("李四", 24));
		list.add(new Student("李四", 24));
		list.add(new Student("李四", 24));
		list.add(new Student("李四", 24));
		
		ArrayList newList=getSingle(list);
		System.out.println(newList);

	}
	public static ArrayList getSingle(ArrayList list){
		ArrayList newList=new ArrayList();	//创建新集合
		Iterator it=list.iterator();		//获取迭代器
		while(it.hasNext()){				
			if(!newList.contains(it.next()))
				newList.add(it.next());		//判断重复,不重复则添加
		}
		return newList;
		
	}

student类

package demo_bean;

public class Student {
	private int age;
	private String name;
	public Student() {
		super();
		
	}
	public Student(String name,int age) {
		super();
		this.age = age;
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String toString() {
		return "Student [age=" + age + ", name=" + name + "]";
	}
	public boolean equals(Object obj) {
		Student stu=(Student)obj;
		return this.name.equals(stu.name)&&this.age==stu.age;//equals方法比较的是地址值,
													//String类里面的equals方法被重写比较的是字符串
		
	}

}

a02cab39175e8f32539531ea21c0d1ab.png

用LinkedList模拟栈结构:

栈结构的特点:先进后出

Stack类

package list;

import java.util.LinkedList;

public class Stack {
	private LinkedList list=new LinkedList();
	/**
	 * 模拟进栈
	 */
	public void in(Object obj){
		list.addFirst(obj);
	}
	/*
	 * 模拟出栈
	 * */
	public Object out(){
		return list.removeFirst();
	}
	/*
	 * 判断栈空
	 */
	public boolean isempty(){
		return list.isEmpty();
	}
	


}

主方法类:

package list;

public class Demo4_LinkedList {

	/* A:案例演示
	* 需求:请用LinkedList模拟栈数据结构的集合,并测试
	* 创建一个类将Linked中的方法封装
	*/ 
	public static void main(String[] args) {
		Stack s=new Stack();
		s.in("a");
		s.in("b");
		s.in("c");
		s.in("d");
		
		while(!s.isempty()){
			System.out.println(s.out());
		}
		
		
	}

}

04e5b99876ac4fc3a63721fece84c0dc.png

泛型的基本使用及通配符

Demo3类

package jdk5;

import java.util.ArrayList;

import bean.LittleStudent;
import bean.Student;

public class Demo3 {
/*
*@author 罗伊
*/
public static void main(String[] args) {
	ArrayList<Student> list1=new ArrayList<>();			//泛型的基本使用
	list1.add(new Student("ly",19));
	list1.add(new Student("ll",19));
	
	ArrayList<LittleStudent> list2=new ArrayList<>();
	list2.add(new LittleStudent("huhu",6));
	list2.add(new LittleStudent("huahua",6));

	list1.addAll(list2);								//泛型通配符collection<? extends E>
														
	System.out.println(list1);
	
}
}

student类

package bean;

public class Student {
	private int age;
	private String name;
	public Student() {
		super();
		
	}
	public Student(String name,int age) {
		super();
		this.age = age;
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String toString() {
		return "Student [age=" + age + ", name=" + name + "]";
	}
	public boolean equals(Object obj) {
		Student stu=(Student)obj;
		return this.name.equals(stu.name)&&this.age==stu.age;//equals方法比较的是地址值,
													//String类里面的equals方法被重写比较的是字符串
		
	}

}

715ea6c70020cd5f9fe72878dcec12cc.png

715ea6c70020cd5f9fe72878dcec12cc.png

715ea6c70020cd5f9fe72878dcec12cc.png

24aeb3711bf172333e1c58f1decf0e43.png

值得注意的是静态方法必须要指定自己的泛型,因为静态方法会随着类的加载而加载,也就是在实例化对象指定泛型之前,如果没有自己的泛型会报错。

ArrayList集合的嵌套使用

Demo1类

package jdk5;

import java.util.ArrayList;

import bean.Student;

public class Demo1 {
	/**
	 * 集合ArrayList嵌套ArrayList
	 */
	public static void main(String[] args) {
		ArrayList<ArrayList<Student>> clazz=new ArrayList<>();//创建嵌套的集合类班级类
		ArrayList<Student> first =new ArrayList<>();		//创建两个班的学生类
		ArrayList<Student> second =new ArrayList<>();
		first.add(new Student("zh",23));					//向每个班中添加学生对象
		first.add(new Student("zh",24));
		first.add(new Student("zh",25));
		first.add(new Student("zh",26));
		second.add(new Student("zh1",23));
		second.add(new Student("zh2",23));
		second.add(new Student("zh3",23));
		second.add(new Student("zh4",23));
		clazz.add(first);									
		clazz.add(second);
		for (ArrayList<Student> student : clazz) {			//用加强for循环遍历输出每一个学生信息
			
				System.out.println(student);
			
			
		}
		
	}	
		
}

LittleStudent类

package bean;

public class LittleStudent extends Student {
	private int age;
	private String name;
	public LittleStudent() {
		super();
		// TODO Auto-generated constructor stub
	}
	public LittleStudent(String name, int age) {
		super(name, age);
		// TODO Auto-generated constructor stub
	}
	

}

Student类同上,运行结果如下:

048c23598ce732392e4b2ffa8c739cd1.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值