11.常用API—ArrayList

2022-01-13  本文已影响0人  每天起床都想摆

ArrayList

集合概述

ArrayList集合快速入门

ArrayList对泛型的支持

泛型概述

ArrayList常用API,遍历

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

import java.util.ArrayList;

public class ArrayListTest {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList();
        list.add("Java");
        list.add("JavaWeb");
        list.add("JavaScript");
        list.add("MySQL");
        list.add("MyBatis");
        list.add("JDBC");
        System.out.println(list);

        // 1. public  E  get(int  index)               | 返回指定索引处的元素
        System.out.println(list.get(2));

        // 2. public  int  size()                      | 返回集合中元素的个数
        System.out.println(list.size());

        // 集合遍历
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }


        // 3. public  E  remove(int  index)            | 删除指定索引处的元素,返回被删除的元素
        System.out.println(list.remove(5));
        System.out.println(list);

        // 4. public  boolean  remove(Object  o)       | 删除指定的元素,返回删除是否成功
        // 当集合中存在相同的元素时,只会删除首次出现的元素
        System.out.println(list.remove("MySQL"));
        System.out.println(list);

        // 5. public  E  set(int  index,  E  element)  | 修改指定索引处的元素,返回被修改的元素
        System.out.println(list.set(0, "PHP")); // 返回的是指定索引的原来的元素
        System.out.println(list);

    }
}

ArrayList集合案例:

ArrayList遍历并删除元素

package com.java.test;

import java.util.ArrayList;

public class ArrayListScore {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        list.add(98);
        list.add(77);
        list.add(66);
        list.add(89);
        list.add(79);
        list.add(50);
        list.add(100);

        // 集合正推出bug因为元素的索引随着删除元素而动态变化着,所以需要倒着循环
        /*                      <---- i
            [98, 77, 66, 89, 79, 50, 100]
            [98, 77, 66, 89, 79, 100]
            [98, 77, 66, 89, 100]
            [98, 77, 89, 100]
            [98, 89, 100]
            从后往前判断,删除元素时,长度-1,上一轮循环判断过的元素重新被判断
         */
        for (int i = list.size() - 1; i >= 0; i--) {
            if (list.get(i) < 80) {
                list.remove(i);
            }
        }
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }
}

对于从集合中遍历元素,并筛选出元素删除它,解决思路:

  • 从集合后面遍历然后删除,可以避免漏掉元素

ArrayList存储自定义类型的对象

ArrayList元素搜索

package com.java.test;

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

    public Student() {
    }

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

    public String getId() {
        return id;
    }

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

    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;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}
package com.java.test;

import java.util.ArrayList;
import java.util.Scanner;

public class ArrayListStudent {
    public static void main(String[] args) {
        ArrayList<Student> students = new ArrayList();
        Student stu1 = new Student("0001", "王宝强", 30, "男");
        students.add(stu1);
        Student stu2 = new Student("0002", "陈羽凡", 31, "女");
        students.add(stu2);
        Student stu3 = new Student("0003", "贾乃亮", 32, "男");
        students.add(stu3);
        Student stu4 = new Student("0004", "谢霆锋", 33, "女");
        students.add(stu4);

        // 遍历已经存储进来的学生信息
        for (int i = 0; i < students.size(); i++) {
            Student stu = students.get(i);
            System.out.print("学号:" + stu.getId() + "\t");
            System.out.print("姓名:" + stu.getName() + "\t");
            System.out.print("年龄:" + stu.getAge() + "\t");
            System.out.print("性别:" + stu.getSex() + "\t");
            System.out.println();
        }
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("输入学号查询学生信息:");
            String Id = scanner.next();
            Student s = getStudentById(students, Id);
            //判断学号是否拥有地址,即是否存在
            if (s == null) {
                System.out.println("查无此人");
            } else {//返回的是对象,通过对象调用方法获得信息
                System.out.println("学号:" + s.getId());
                System.out.println("姓名:" + s.getName());
                System.out.println("年龄:" + s.getAge());
                System.out.println("性别:" + s.getSex());
            }
        }

    }

    public static Student getStudentById(ArrayList<Student> students, String strId) {
        for (int i = 0; i < students.size(); i++) {
            Student s = students.get(i);
            if (s.getId().equals(strId)) {
                return s;
            }
        }
        return null;    // 查无此学号
    }
}
上一篇下一篇

猜你喜欢

热点阅读