博客
关于我
Java中ArrayList集合嵌套存储和遍历
阅读量:742 次
发布时间:2019-03-17

本文共 2424 字,大约阅读时间需要 8 分钟。

Java 模型类及集合嵌套遍历实现

学生类结构

该学生类通过私有字段实现对象数据封装,包含姓名和年龄两个属性。通过公有无参数、单参数构造方法支持对象的初始化。以下是该类的具体实现:

public class Student {    private String name;    private int age;    public Student() {        super();    }    public Student(String name, int age) {        super();        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;    }    @Override    public int hashCode() {        final int prime = 31;        int result = 1;        result = prime * result + age;        result = prime * result + ((name == null) ? 0 : name.hashCode());        return result;    }    @Override    public boolean equals(Object obj) {        if (this == obj)            return true;        if (obj == null)            return false;        if (getClass() != obj.getClass())            return false;        Student other = (Student) obj;        if (age != other.age)            return false;        if (name == null) {            if (other.name != null)                return false;        } else if (!name.equals(other.name))            return false;        return true;    }}

集合嵌套遍历实现

基于上述学生类,本文设计了一个集合嵌套遍历的解决方案,适用于班级管理场景。具体实现如下:

import java.util.ArrayList;public class Test1 {    public static void main(String[] args) {        ArrayList bigArrayList = new ArrayList();        ArrayList firstArrayList = new ArrayList();        ArrayList secondArrayList = new ArrayList();        // 添加学生信息        Student s1 = new Student("唐僧", 21);        Student s2 = new Student("沙僧", 22);        Student s3 = new Student("猪八戒", 23);        // 第一个班级信息        firstArrayList.add(s1);        firstArrayList.add(s2);        firstArrayList.add(s3);        bigArrayList.add(firstArrayList);        // 第二个班级信息        Student s4 = new Student("曹操", 18);        Student s5 = new Student("刘备", 40);        Student s6 = new Student("孙权", 28);        secondArrayList.add(s4);        secondArrayList.add(s5);        secondArrayList.add(s6);        bigArrayList.add(secondArrayList);        // 遍历输出        for (ArrayList array : bigArrayList) {            for (Student s : array) {                System.out.println(s.getName() + "---" + s.getAge());            }        }    }}

以上实现通过多层嵌套集合结构,确保了班级间学生信息的独立管理和整体容斥处理。通过这种方式,不同班级的学生可以在同一顶级集合中统一管理,实现灵活的数据扩展和维护。

转载地址:http://vgcez.baihongyu.com/

你可能感兴趣的文章
Objective-C实现最小二乘多项式曲线拟合(附完整源码)
查看>>
Objective-C实现最快的归并排序算法(附完整源码)
查看>>
Objective-C实现最长公共子序列算法(附完整源码)
查看>>
Objective-C实现最长回文子序列算法(附完整源码)
查看>>
Objective-C实现最长子数组算法(附完整源码)
查看>>
Objective-C实现最长字符串链(附完整源码)
查看>>
Objective-C实现最长递增子序列算法(附完整源码)
查看>>
Objective-C实现有限状态机(附完整源码)
查看>>
Objective-C实现有限状态自动机FSM(附完整源码)
查看>>
Objective-C实现有限集上给定关系的自反关系矩阵和对称闭包关系矩阵(附完整源码)
查看>>
Objective-C实现朴素贝叶斯算法(附完整源码)
查看>>
Objective-C实现杰卡德距离算法(附完整源码)
查看>>
Objective-C实现极值距离算法(附完整源码)
查看>>
Objective-C实现构造n以内的素数表(附完整源码)
查看>>
Objective-C实现某文件夹下文件重命名(附完整源码)
查看>>
Objective-C实现查找整数数组中给定的最小数字算法(附完整源码)
查看>>
Objective-C实现根据cpu和磁盘序列号生成注册码( 附完整源码)
查看>>
Objective-C实现格雷码序列算法(附完整源码)
查看>>
Objective-C实现桥接模式(附完整源码)
查看>>
Objective-C实现检查给定字符串是否在camelCase中算法(附完整源码)
查看>>