2021-09-02 集合基础知识

集合基础

1 集合和数组以及StringBuilder的特点对比

  • 共同点 : 都是存储数据的容器,打印出来的不是地址值而是容器里面的内容
  • 不同点 :
长度存储数据类型
数组数组一旦被初始化,数组的长度固定不变可以存储基本数据类型也可以存储引用数据类型
StringBuilder长度可变任意类型
集合长度可变只能存储引用数据类型,要想存储基本数据类型需要存储对应的包装类ArrayList

注意 : StringBuilder不管存储什么类型的数据,在容器中都是以字符串形式存在!!!

2 关于ArrayList集合

ArrayList :

  • 是一个长度可变的容器
  • :是一种特殊的数据类型,泛型,只能是引用数据类型
  • 泛型的作用:规范集合中存储元素的数据类型

使用方法 :

​ 在出现E的地方我们使用引用数据类型即可

​ 举例: ArrayList,ArrayList

3 ArrayList的构造方法和添加方法

方法名说明
public ArrayList()创建一个空的集合对象
public boolean add(E e)将指定的元素追加到此集合的末尾
public void add(int index,E element)向集合中的指定位置插入指定的元素

4 ArrayList集合常用方法

方法名说明
public boolean remove(Object o)删除指定的元素,返回删除是否成功
public E remove(int index)删除指定索引处的元素,返回被删除的元素
public E set(int index,E element)修改指定索引处的元素,返回被修改的元素
public E get (int index)返回指定索引处的元素
public int size()返回集合中的元素的个数

5 ArrayList 一些代码展示

5.1.1 案例需求

创建一个存储字符串的集合,存储3个字符串元素,使用程序实现在控制台遍历该集合

5.1.2 代码实现

import java.util.ArrayList;
public class ArrayTest {
    public static void main(String[] args) {
        // 创建集合对象
        ArrayList<String> list = new ArrayList<>();

        // 添加元素
        list.add("孔明");
        list.add("庞统");
        list.add("郭嘉");
        list.add("荀彧");
        list.add("陆逊");
        list.add("周瑜");

        // 遍历集合
        // System.out.println(list);// [孔明, 庞统, 郭嘉, 荀彧, 陆逊, 周瑜]
//        System.out.println(list.get(0));
//        System.out.println(list.get(1));
//        System.out.println(list.get(2));
//        System.out.println(list.get(3));
//        System.out.println(list.get(4));
//        System.out.println(list.get(5));

        // for循环拿到集合中每一个元素索引
        for (int i = 0; i < list.size(); i++) {
            // 根据索引获取每一个元素
            String s = list.get(i);
            System.out.println(s);
        }

    }
}

5.2.1 案例需求

创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合

5.2.2 代码实现

import java.util.ArrayList;
public class ArrayTest {
    public static void main(String[] args) {
        //    2 创建集合对象
        ArrayList<Student> list = new ArrayList<>();// {s1 , s2 , s3....}
        //    3 创建学生对象
        Student s1 = new Student("曹操", 50);
        Student s2 = new Student("曹昂", 30);
        Student s3 = new Student("曹丕", 29);
        Student s4 = new Student("曹植", 28);
        Student s5 = new Student("曹彰", 27);
        Student s6 = new Student("曹熊", 26);
        Student s7 = new Student("曹冲", 10);
        //    4 添加学生对象到集合中
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        list.add(s5);
        list.add(s6);
        list.add(s7);
        //    5 遍历集合,采用通用遍历格式实现
        for (int i = 0; i < list.size(); i++) {
            // 根据索引获取每一个元素
            Student s = list.get(i);
            System.out.println(s.getName() + "---" + s.getAge());
        }
    }
}

5.3.1 案例需求

创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合
学生的姓名和年龄来自于键盘录入

5.3.2 代码实现

import java.util.ArrayList;
import java.util.Scanner;
public class ArrayTest {
    public static void main(String[] args) {
        // 2 创建集合对象
        ArrayList<Student> list = new ArrayList<>();

        Scanner sc = new Scanner(System.in);

        // 键盘录入学生对象所需要的数据 , 录入3个
        for (int i = 1; i <= 3; i++) {
            System.out.println("请输入第" + i + "学生的名字:");
            String name = sc.nextLine();
            System.out.println("请输入第" + i + "学生的年龄:");
            int age = sc.nextInt();
            sc.nextLine();// 抵消回车字符

            // 创建学生对象 , 学生的名字和年龄键盘录入而来
            Student s = new Student(name, age);

            // 把学生对象添加到集合中
            list.add(s);
        }

        // 遍历集合
        for (int i = 0; i < list.size(); i++) {
            // 根据索引获取集合中每一个元素
            Student s = list.get(i);
            System.out.println(s.getName() + "---" + s.getAge());
        }
    }
}

5.4.1 案例需求

创建一个存储String的集合,内部存储(test,张三,李四,test,test)字符串
删除所有的test字符串,删除后,将集合剩余元素打印在控制台

5.4.2 代码实现

import java.util.ArrayList;
public class ArrayTest {
    public static void main(String[] args) {
        // 1 创建集合对象
        ArrayList<String> list = new ArrayList<>();

        // 2 调用add方法,添加字符串
        list.add("test");
        list.add("张三");
        list.add("李四");
        list.add("test");
        list.add("test");

        // 遍历集合 , 拿到每一个元素做判断
        // [张三,李四,test]
        for (int i = 0; i < list.size(); i++) {
            // 根据索引获取元素值
            String s = list.get(i);
            if(s.equals("test")){
                list.remove(i);
                i--;
            }
        }
       for (int i = 0; i < list.size(); i++) {
            String s = list.get(i);
            System.out.println(s);
        }
    }
}

思路二:

import java.util.ArrayList;
public class ArrayTest {
    public static void main(String[] args) {
        // 1 创建集合对象
        ArrayList<String> list = new ArrayList<>();

        // 2 调用add方法,添加字符串
        list.add("test");
        list.add("张三");
        list.add("李四");
        list.add("test");
        list.add("test");
	   for (int i = list.size() - 1; i >= 0; i--) {
            // 根据索引获取元素值
            String s = list.get(i);
           if (s.equals("test")) {
                list.remove(i);
            }
        }
        for (int i = 0; i < list.size(); i++) {
            String s = list.get(i);
            System.out.println(s);
        }
    }
}        

5.5.1 案例需求

定义一个方法,方法接收一个集合对象(泛型为Student),方法内部将年龄低于18的学生对象找出
并存入新集合对象,方法返回新集合。

5.5.2 代码实现

/*
    学生类
 */
public class Student {
    private String name;
    private int age;

    public Student() {
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
/*
    学生测试类
 */
import java.util.ArrayList;
public class ArrayTest {
    public static void main(String[] args) {
        // 1 创建集合对象
        ArrayList<Student> list = new ArrayList<>();
        // 2 创建学生对象
        Student s1 = new Student("曹操", 50);
        Student s2 = new Student("曹昂", 30);
        Student s3 = new Student("曹丕", 29);
        Student s4 = new Student("曹植", 28);
        Student s5 = new Student("曹彰", 17);
        Student s6 = new Student("曹熊", 16);
        Student s7 = new Student("曹冲", 10);
        // 3 把学生对象添加到集合中
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        list.add(s5);
        list.add(s6);
        list.add(s7);

        // 调用方法 , 接收方法的返回值
        ArrayList<Student> newList = getList(list);

        // 遍历方法的返回的新集合
        for (int i = 0; i < newList.size(); i++) {
            Student s = newList.get(i);
            System.out.println(s.getName() + "----" + s.getAge());
        }
    }


    // 方法的功能: 接收一个集合 , 把集合中年龄小于18岁的学生对象存储到新的集合中, 并返回
    public static ArrayList<Student> getList(ArrayList<Student> list) { // list = {s1 , s2 , s3}
        // 方法内部定义新集合,准备存储筛选出的学生对象 ArrayList<Student> newList
        ArrayList<Student> newList = new ArrayList<>();

        // 遍历原集合,获取每一个学生对象
        for (int i = 0; i < list.size(); i++) {
            // 根据索引获取每一个元素
            Student s = list.get(i);
            // 通过学生对象调用getAge方法获取年龄,并判断年龄是否低于18
            if (s.getAge() < 18) {
                // 将年龄低于18的学生对象存入新集合
                newList.add(s);
            }
        }
        return newList;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值