简体   繁体   English

如何显示对象中的项目的数组列表?

[英]how to display an arraylist of items within an object?

i want to display a list of screens in an array alongside the name of that screen. 我想在屏幕名称旁边显示数组中的屏幕列表。

here is the code for the Menu class 这是Menu类的代码

Scanner input = new Scanner(System.in); 扫描仪输入=新的Scanner(System.in);

Screen [] screen;


    public void menu()
    {


        screen[0] = new Screen("SRN-1",{"The Hunger Games: Mockingjay, Part 2", "Love the Coopers", "The 33"}, {6.78, 7.89, 5.75}, {21,23,26});
        screen[1] = new Screen("SRN-2",{"Kilo Two Bravo ", "Spectre (2015)", "The Peanuts Movie (2015)"}, {7.45,8.37,8.95}, {12,76,75});
        screen[2] = new Screen("SRN-3", {"Goosebumps (2015)", " Bridge of Spies (2015)", "  Hotel Transylvania 2"}, {8.37,8.58,9.58}, {63,63,78});
        screen[3] = new Screen("SRN-4", {"  The Last Witch Hunter (2015)", "Paranormal Activity: The Ghost Dimension (2015)", "The Intern (2015)"}, {5.76,7.37,9.47}, {63,84,13});

        prt("");
        prt("Select a screen: ");

        for(int i = 0; i < screen.length; ++i){
            prt("Press " + i + " to select " + screen[i].getName();
        }

        int inputNo = input.nextInt();

and here is the code for the screen class. 这是屏幕类的代码。 What is wrong with my constructor? 我的构造函数有什么问题?

public class Screen {

    String srnName;
    String [] movieList;

    double [] cost;

    int [] seats;

    public Screen(String srnName, String [] movieList, double [] cost, int [] seats)

   {this.srnName = srnName;
    this.cost = cost;
    this.seats = seats;
    this.movieList = movieList;}

    public int[] getSeat() {return seats;}
    public String getName() {return srnName;}
    public String[] getMovie() {return movieList;}
    public double[] getCost() {return cost;}


}

1. You must define a size of array. 1.您必须定义数组的大小。

Screen[] screen = new Screen[4];

2. If you pass array as parameter you must add type: 2.如果将数组作为参数传递,则必须添加类型:

screen[0] = new Screen("SRN-1", new String[]{"The Hunger Games: Mockingjay, Part 2", "Love the Coopers", "The 33"}, new double[]{6.78, 7.89, 5.75}, new int[]{21,23,26});

Or you can initialize variable to pass as parameters: 或者,您可以初始化变量以作为参数传递:

String[] movieList1 = {"The Hunger Games: Mockingjay, Part 2", "Love the Coopers", "The 33"};
double[] cost1 = {6.78, 7.89, 5.75};
int[] seats1 = {21,23,26};
screen[0] = new Screen("SRN-1", movieList1, cost1, seats1);

3. You have forgotten about closing parenthesis: 3.您忘记了右括号:

 prt("Press " + i + " to select " + screen[i].getName());

Ad 1. You can use List instead od array. 广告1.您可以使用列表代替od数组。 Then you will not have define size array fixed. 这样,您将没有固定的定义大小数组。

List<Screen> screen = new ArrayList<>();
screen.add(new Screen("SRN-1", new String[]{"The Hunger Games: Mockingjay, Part 2", "Love the Coopers", "The 33"}, new double[]{6.78, 7.89, 5.75}, new int[]{21,23,26}));
// etc. you can add multiple objects

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

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