Java基础(6)---------List接口

List接口

特点:

1.它是一个元素存取有序的集合。例如,存元素的顺序是11、22、33。那么集合中,元素的存储就是按照11、22、33的顺序完成的)。

2.它是一个带有索引的集合,通过索引就可以精确的操作集合中的元素(与数组的索引是一个道理)。
3.集合中可以有重复的元素,通过元素的equals方法,来比较是否为重复的元素

List接口中常用方法
方法名说明
public void add(int index, E element)将指定的元素,添加到该集合中的指定位置上。
public E get(int index)返回集合中指定位置的元素
public E remove(int index)移除列表中指定位置的元素, 返回的是被移除的元素。
remove(Object o)删除对象,
public E set(int index, E element)用指定元素替换集合中指定位置的元素,返回值的更新前的元素。
ArrayList集合

java.util.ArrayList 集合数据存储的结构是数组结构。元素增删慢,查找快,由于日常开发中使用最多的功能为查询数据、遍历数据,所以 ArrayList 是最常用的集合。

案例1

代码实现:

·· 学生类

package Day0816am;

public class Student {
	private String name;
	private double score;
	
	
	public Student() {
	}
	public Student(String name, double score) {
		super();
		this.name = name;
		this.score = score;
	}
	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;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", score=" + score + "]";
	}
}

··· 测试类

@Test
	public void test1(){
		ArrayList<Student> stu = new ArrayList<Student>();
		stu.add(new Student("张三",98.0));
		stu.add(new Student("李四",98.0));
		stu.add(new Student("王五",98.0));
		
		double sum = 0.0;
		double avg = 0.0;
		//增强for循环
		System.out.println("增强for循环");
		for(Student s:stu){
			System.out.println(s);
		}
		Iterator it=stu.iterator();
		//
		//用while循环改进元素的判断和获取
		System.out.println("迭代器");
		while (it.hasNext()) {
			Student s = (Student) it.next();
			sum += s.getScore();
			System.out.println(s);
		}
		System.out.println("总成绩为:"+sum);
		System.out.println("总成绩为:"+sum/stu.size());
	}

案例2

功能描述:
使用ArrayList保存学生信息(学号,姓名,年龄,成绩),要求删除指定学生后遍历输出。

思路分析:
1、创建三个学生对象保存到集合
2、删除一个对象
3、遍历集合

— 学生类

package Day0816am;

public class Student {
	private String name;
	private double score;
	private String id;
	private int age;
	
	
	public Student() {
	}
	
	public Student(String name, double score, String id, int age) {
		super();
		this.name = name;
		this.score = score;
		this.id = id;
		this.age = age;
	}

	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 getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + ", score=" + score + ", id=" + id + ", age=" + age + "]";
	}
}

— 测试类

public void test3(){
		ArrayList<Student> stu = new ArrayList<Student>();
		stu.add(new Student("张三",98.0,"001213",18));
		stu.add(new Student("李四",97.0,"565956",19));
		stu.add(new Student("王五",88.0,"365459",65));
		
		for(Student s : stu){
			System.out.println(s);
		}
		//删除,通过索引删除
		System.out.println("删除元素后");
		stu.remove(1);
		for(Student s : stu){
			System.out.println(s);
		}
		/*
		运行结果:	
		Student [name=张三, score=98.0, id=001213, age=18]
		Student [name=李四, score=97.0, id=565956, age=19]
		Student [name=王五, score=88.0, id=365459, age=65]
		删除元素后
		Student [name=张三, score=98.0, id=001213, age=18]
		Student [name=王五, score=88.0, id=365459, age=65]
		 */
	}
LinkedList集合

java.util.LinkedList 集合数据存储的结构是链表结构。方便元素添加、删除的集合。

方法名说明
public void addFirst(E e)在该列表开头插入指定的元素
public void addLast(E e)将指定的元素追加到此列表的末尾
public E getFirst()返回此列表中的第一个元素
public E getLast()返回此列表中的最后一个元素
public E removeFirst()从此列表中删除并返回第一个元素
public E removeLast()从此列表中删除并返回最后一个元素
public E pop()从此列表所表示的堆栈处弹出一个元素。
public void push(E e)将元素推入此列表所表示的堆栈。
public boolean isEmpty()如果列表不包含元素,则返回true。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

?abc!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值