ArrayListへのオブジェクトの格納と取り出し

概要

自作の従業員クラスEmployeeのオブジェクトをArrayListのオブジェクトに格納し、取り出し、削除を行う。

import java.util.ArrayList;

class Employee {
    private int    number;
    private String name;

    public Employee(int number, String name) {
        this.number = number;
        this.name = name;
    }

    public int getNumber() {
        return this.number;
    }

    public String getName() {
        return this.name;
    }
}

public class EmployeeList {
    public static void main(String[] args) {

        Employee satou = new Employee(123, "佐藤");
        Employee itou = new Employee(456, "伊藤");
        Employee katou = new Employee(789, "加藤");
        Employee kantou = new Employee(321, "菅藤");
        Employee shutou = new Employee(322, "首藤");
        Employee gondou = new Employee(323, "権藤");
        Employee kawatou = new Employee(324, "川藤");

        ArrayList<Employee> list = new ArrayList<Employee>();

        list.add(satou);
        list.add(itou);
        list.add(katou);
        list.add(shutou);
        list.add(gondou);
        list.add(kawatou);
        System.out.println(list.size() + "人を登録しました。");
        System.out.println("下記の人物が登録されています。");
        System.out.println("-----------------------");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getNumber() + ":" + list.get(i).getName());
        }
        System.out.println("-----------------------");
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getName() == "伊藤") {
                System.out.println("伊藤さんの登録位置:" + i);
            }
        }
        System.out.println("川藤さんがリストに含まれているか(true/false)");
        if (list.contains(kawatou)) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }
        System.out.println("菅藤さんがリストに含まれているか(true/false)");
        if (list.contains(kantou)) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }
        System.out.println("伊藤さんを削除します。");
        list.remove(itou);
        System.out.println(list.size() + "人を登録済みです。");
        System.out.println("下記の人が登録されています。");
        System.out.println("----------------------");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getNumber() + ":" + list.get(i).getName());
        }
        System.out.println("----------------------");
    }

}

実行結果

6人を登録しました。
下記の人物が登録されています。

                                            • -

123:佐藤
456:伊藤
789:加藤
322:首藤
323:権藤
324:川藤

                                            • -

伊藤さんの登録位置:1
川藤さんがリストに含まれているか(true/false)
true
菅藤さんがリストに含まれているか(true/false)
false
伊藤さんを削除します。
5人を登録済みです。
下記の人が登録されています。

                                          • -

123:佐藤
789:加藤
322:首藤
323:権藤
324:川藤

                                          • -

今押さえるべきマイナンバー理解のカギ

今押さえるべきマイナンバー理解のカギ