Java工作学习----第十课 Java中的集合 Collection体系中的List接口中的ArrayList 2021.2.2

集合

集合是什么?

对象的容器,定义了对多个对象进行操作的常用方法,实现数组的功能。

与数组的区别:

  • 数组长度是固定的,集合长度不固定的

  • 数组可以存储基本类型,而集合只能存储引用类型

集合所处包的位置:java.util.*

Collection体系集合

image-20210202175128560

Collection是一个跟接口

List接口:有序 有下标,元素可以重复

Set接口:无序,无下标元素不能重复

以上三个都是接口,不能直接创建对象

Collection父接口

特点:代表一组任意类型的对象,无序 无下标 不能重复

方法:

image-20210202175450894

几个方法的使用

//创建集合,因为Collection不能直接创建集合,此下边包含的Array也不行
Collection collection = new ArrayList();
//添加元素
collection.add("apple");
collection.add("xigua");
collection.add("榴莲");
//查看元素个数 使用size命令
System.out.println("元素个数:" + collection.size());
//print重写了对于数组的写出方法,可以直接输出
System.out.println(collection);
//删除元素
collection.remove("榴莲");
System.out.println(collection);
// collection.clear();
//清空元素
 System.out.println(collection);

迭代器

这里引用了迭代器的概念

使用迭代器,转来用来遍历结合的的,迭代的意思就是循环或者遍历
//Iterator 是一个接口,故也不能new一个对象

hasNext

next

remove三个方法
//迭代过程中不能使用collection其他的操作,不允许删除,若非要删除必须使用Iterator里边

// 遍历元素 1.使用增强for
for (Object object:collection
     ) {
    System.out.println(object);
}
//遍历元素 2.使用迭代器,转来用来遍历结合的的,迭代的意思就是循环或者便利
//Iterator 是一个接口  hasNext next remove三个方法
//迭代过程中不能使用collection其他的操作,不允许删除,若非要删除必须使用Iterator里边的
Iterator iterator = collection.iterator();
while (iterator.hasNext()) {
    String str=(String) iterator.next();
    System.out.println(str);
}
//判断
  // contains 适合
  System.out.println(collection.contains("apple"));
  System.out.println(collection.isEmpty());

用数组存储学生信息

这是测试函数

package cllect;

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

/**
 * 用Collection类来保存学生信息
 *
 */
public class domo2 {
    public static void main(String[] args) {
        //新建conllection对象
        Collection collection = new ArrayList();
        Student student1 = new Student("张三",18);
        Student student2 = new Student("李四",18);
        Student student3 = new Student("王五",18);
        //将学生对象,集合起来添加到Collection集合当中
        collection.add(student1);
        collection.add(student2);
        collection.add(student3);
        System.out.println(collection.size());
//尽管我们并没有管理collection之间的 但是输出的时候还是调用了toString,还好我们重写了方法
        System.out.println(collection.toString());
        collection.remove(student1);
        System.out.println("删除之后"+collection.size());
        //建立collection对象实际是建立的地址
        //3 遍历 增强for
        for (Object o : collection) {
            Student str = (Student) o;
            System.out.println(str.toString());
           // System.out.println(o);//这里的class文件很容易看出来是进行一个迭代了
          // 遍历 迭代器
            Iterator iterator = collection.iterator();
            while (iterator.hasNext()) {
                Student o1 =  (Student)iterator.next();
                System.out.println(o1);

            }


        }


    }
}

这是学生类

重写了构造器,重写了toString

package cllect;

public class Student {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

List 接口

特点:有序的,有下标

image-20210202194752154

插入,删除,判断跟上边的Collection一样

在遍历的时候多了一个可以使用下角标遍历的实现方式,//增强for,跟迭代器 Iterator

//遍历比起collection中多了 直接使用下标遍历,但是它不存在[]符号必须使用get方法
for (int i = 0; i < list.size() ; i++) {
  System.out.println(list.get(i));
}

除此之外还存在一个向后遍历

//使用迭代器从前向后遍历集合 previousIndex这个遵循数组的遍历方式
while (listIterator.hasPrevious()) {

    Object nextElement =  listIterator.previous();
    String o1 = (String) listIterator.previous();

//System.out.println(listIterator.nextIndex());
//使用previousIndex,从后往前遍历数组

System.out.println(o1);
System.out.println(listIterator.previousIndex());
}
 //多了一个方法 获取位置,遵循数组的命名规范
System.out.println(list.indexOf("apple"));

如果加的对象是数字 进行一个装箱,remove(下标)

如果要用的话需要把这个数字

remove(new Integer(20));

//得到集合,从哪里断开,含头不含尾

   List sublist= list.subList(0,2);
    System.out.println(sublist);
常用实现类

ArrayList:查询快,增删慢,数组结构实现,线程不安全

LinkedList :链表结构实现,增删快,查询慢

ArrayList

对于equals重写的应用

image-20210202211806096

回到内部类的equals:

if (this == o) return true;
 if (o == null || getClass() != o.getClass()) return false;
 Student student = (Student) o;
 return name.equals(student.name);

其他的使用方法

增加 add

删除remove

遍历 四种方法{数组,增强for,迭代器,列表},在上边的List那边均做过演示

几个常用的常量:

**没有像集合添加元素时容量为0,添加以后变成10

DEFAULT_CAPACITY:默认容量大小

elementData :存放元素的数组

size:实际元素的个数

add方法

扩容:每次都是原来的1.5倍

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

秀才大大

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

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

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

打赏作者

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

抵扣说明:

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

余额充值