Java进阶篇10——ArrayList+LinkedList

1. 集合类总纲

在这里插入图片描述

2. ArrayList

2.1 性质

  • List 接口,元素有顺序。
  • List 继承 Collection 接口。
  • ArrayList类实现 List接口。
  • 可以理解为 ArrayList 为动态数组。
  • 向 List 中间插入或移除元素的速度很慢。
  • ArrayList 不宜进行频繁的增删。

2.2 ArrayList 构造与集合元素的读写

	/**
	 * 有序,有重复
	 */
	public static void practice1() {
		List<Student> list = new ArrayList<Student>();

		Student s1 = new Student("1001", "chen", 18);
		Student s2 = new Student("1001", "luo", 25);
		Student s3 = new Student("1003", "zhang", 25);
		Student s4 = new Student("1004", "pi", 20);
		Student s5 = new Student("1002", "wang", 25);
		Student s6 = new Student("1001", "chen", 18);

		list.add(s1);
		list.add(s2);
		list.add(s3);
		list.add(s4);
		list.add(s5);
		list.add(s6);

		Iterator<Student> it = list.iterator();
		while (it.hasNext()) {
			System.out.println(it.next());
		}
	}
1001 chen 18
1001 luo 25
1003 zhang 25
1004 pi 20
1002 wang 25
1001 chen 18

2.3 通过下标遍历

		for(int i=0; i<arraylist.size(); i++) {
			Student s = arraylist.get(i);
			System.out.println(s);
		}

2.4 删除元素

		arraylist.remove(0);
		arraylist.remove(s2);

3. LinkedList

3.1 性质

  • LinkedList 与 ArrayList 相反。
  • 有序,有重复。
  • 适合用来增加和移除元素。
  • 但随即访问较慢。
  • 可以通过 LinkedList 来实现栈 Stack 或 队列 Queue。
  • LinkedList 中 addFirst(), addLast(), getFirst(), getLast(), removeFirst(), removeLast()。

3.2 集合 LinkedList 使用样例

	/**
	 * LinkedList 几种常用的增加,删除
	 */
	public static void practice1() {
		LinkedList<String> linkedlist = new LinkedList<String>();
		
		linkedlist.add("a");
		linkedlist.add("b");
		linkedlist.add("c");
		linkedlist.addFirst("x");
		linkedlist.addLast("y");
		
		for(int i=0; i<linkedlist.size(); i++) {
			String str = linkedlist.get(i);
			System.out.println(str);
		}
	}
x
a
b
c
y
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

姜满月

鼓励,鼓励,更加努力

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

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

打赏作者

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

抵扣说明:

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

余额充值