简体   繁体   English

二维ArrayList Java(Gui)

[英]Two Dimensional ArrayList Java (Gui)

I am trying to create program that holds the details of various instruments (eg guitars, keyboards). 我正在尝试创建一个包含各种乐器(例如吉他,键盘)详细信息的程序。 I want it to have information on each item in the Arraylist. 我希望它具有有关Arraylist中每个项目的信息。 I want each instrument to have a Manufacturer and a "description". 我希望每个乐器都有一个制造商和一个“说明”。 I am not new to 2D arrays, however i do not know how to use it in Array lists. 我不是二维数组的新手,但是我不知道如何在数组列表中使用它。 I have given it an attempt. 我已经尝试过了。 (Ps i need to be able to access all of these things to be able to put into a gui. I hope that made sense.) (请注意,我需要能够访问所有这些内容才能放入gui。我希望这是有道理的。)

public class Main {

    public static Login form = new Login();
    public static ArrayList<ArrayList<String>> instt = new ArrayList<ArrayList<String>>();
    public static ArrayList<String> row = new ArrayList<String>();

    public static void main(String[] args) {
        row.add("Chelo");
        row.add("Drums");
        row.add("Flute");
        row.add("Guitar");
        row.add("Harp");
        row.add("Piano");
        row.add("Recorder");
        row.add("Trombone");
        row.add("Trumpet");
        row.add("Xylophone");
        instt.add(row);

        form.setVisible(true);
    }
}

Any suggestions? 有什么建议么? Thank you. 谢谢。

The best approach would be to create an Instrument-Class containing all your information. 最好的方法是创建一个包含所有信息的工具类。

public static ArrayList<Instrument> row = new ArrayList<Instrument>();

public static void main(String[] args){
    row.add(new Instrument("Guitar","Stackoverflow-Instruments","Best guitar!!"));
    row.add(new Instrument("Piano","Stackoverflow-Instruments","Best piano!!"));
}

public static class Instrument{
    private String name;
    private String manufacturer;
    private String description;

    public String getName() {
        return name;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public String getDescription() {
        return description;
    }

    public Instrument(String name, String manufacturer, String description) {
        this.name = name;
        this.manufacturer = manufacturer;
        this.description = description;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM