如何为主数组列表的元素创建“子数组列表”?

时间:2019-04-01 22:49:53

标签: java arraylist sub-array

我正在尝试创建一个程序来显示车辆的“待办事项”列表。这是我希望输出看起来像的示例。

 Blue Spitfire
      Sanding
           Hood
           Doors
      Engine
           Oil pan seal
      Electrical
           Oil pressure light
           Headlight switch

现在,我有一个ArrayList来存储车辆名称(Blue Spitfire)。我想知道如何在每个工作编号下存储工作名称(砂光,发动机,电气)和详细信息(机盖,门,油底壳密封等)。

总而言之,车辆名称存储作业,而作业存储详细信息。我该怎么做?

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您可以使用Map(特别是HashMap)存储键和值之间的关联列表,并使用List或Set(特别是HashSet)存储值列表。

但是正如评论中所指出的那样,最好结合使用该类和类。

这是一种可能的实现。 (您可以使用实用工具类来创建Sets和Maps,例如使用Google Guava库或Apache Commons库中的工具,使其更短。)

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

class Job {
    String model;
    Map<String, Set<String>> areas;

    public Job(String model, Map<String, Set<String>> areas) {
        this.model = model;
        this.areas = areas;
    }

}

public class Example {

    public static void main(String[] args) {
        Map<String, Set<String>> areas = new HashMap<>();
        Set<String> sandingProblems = new HashSet<>();
        sandingProblems.add("Hood");
        sandingProblems.add("Doors");
        areas.put("Sanding", sandingProblems);
        Set<String> engineProblems = new HashSet<>();
        engineProblems.add("Oil panel seal");
        areas.put("Engine", engineProblems);
        Set<String> electricalProblems = new HashSet<>();
        electricalProblems.add("Oil pressure light");
        electricalProblems.add("Headlight switch");
        areas.put("Electrical", electricalProblems);
        Job job = new Job("Blue Spitfire", areas);

        System.out.println(job.model);
        for (Map.Entry<String, Set<String>> entry : areas.entrySet()) {
            System.out.println("\t" + entry.getKey());
            for (String problem : entry.getValue()) {
                System.out.println("\t\t" + problem);
            }
        }
    }
}

答案 1 :(得分:0)

希望这会有所帮助。

import java.util.ArrayList;

class Vechicle {

    Vechicle(String vName) {
        vehicleName= vName;
    }
    String vehicleName;
    ArrayList<Job> jobs = new ArrayList<>();

    void addJob(Job j) {
        jobs.add(j);
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        String returnString = vehicleName ;
        for (Job j: jobs) {
            returnString = returnString+ "\r\n\t"+ j.jobName;
            for (String jobSpecifics: j.jobsSpecifics) {
                returnString = returnString+ "\r\n\t\t"+ jobSpecifics;
            }
        }
        return returnString;
    }
}

class Job {
    String jobName;
    ArrayList<String> jobsSpecifics = new ArrayList<>();
    Job(String jobName) {
        this.jobName= jobName;
    }
    void addJobSpecifics(String s) {
        jobsSpecifics.add (s);
    }
}

public class VechicleMain {

    public static void main(String[] args) {

        Vechicle v = new Vechicle("Blue Spitfire");
        Job j= new Job("Sanding");
        j.addJobSpecifics("Hood");
        j.addJobSpecifics("Doors");
        v.addJob(j);
        System.out.println(v);
        }
}

输出:

enter image description here