【问题标题】:Displaying a multiple object arraylist?显示多对象数组列表?
【发布时间】:2017-05-08 05:37:54
【问题描述】:

我很好奇如何在 Java 中显示多对象数组列表。数组列表同时包含 string 和 int。

这是我正在为其创建数组列表的类。

public class Movie
{

    public int rating;
    public String title, directorName, actorOne, actorTwo, actorThree;


    public Movie(String Title, String Director, String ActorOne, String ActorTwo, String ActorThree, int Rating)
    {
        title = Title;
        directorName = Director;
        actorOne = ActorOne;
        actorTwo = ActorTwo;
        actorThree = ActorThree;
        rating =  Rating;
    }


    public void setTitle(String Title)
    {

        title = Title;

    }

    public String getTitle()
    {
        return title;
    }

        public void setDirector(String Director)
    {

        directorName = Director;

    }

    public String getDirector()
    {
        return directorName;
    }

     public void setActorOne(String ActorOne)
    {

        actorOne = ActorOne;

    }

    public String getActorOne()
    {
        return actorOne;
    }

        public void setActorTw0(String ActorTwo)
    {

        actorTwo = ActorTwo;

    }

    public String getActorTwo()
    {
        return actorTwo;
    }

        public void setActorThree(String ActorThree)
    {

        actorThree = ActorThree;

    }

    public String getActorThree()
    {
        return actorThree;
    }
}

这是我的主要课程

import java.io.*;
import java.util.*;


public class Database
{

    Scanner input = new Scanner(System.in);
    boolean start;
    ArrayList<Movie> movie = new ArrayList<Movie>();
    String title, director, actOne, actTwo, actThree;
    int rating;
    public Database()
    {
        // initialise instance variables
        this.display();
    }
    public void display()                   
    {
        while (start = true)            
        {
            System.out.println("Welcome to the Movie Database");
            System.out.println
            (
                    "Select an option \n" +
                    "1 Find Movie \n" +
                    "2 Add Movie \n" +
                    "3 Delete Movie \n" +
                    "4 Display Favourtite Movies \n" +
                    "5 Exit Database \n" 

            );
            int choice = input.nextInt();
            input.nextLine();
            switch(choice)                      
                {
                    case 1:

                        break;
                    case 2:
                        this.addMovie();
                        break;
                    case 3:

                        break;
                    case 4:

                        break;
                    case 5:
                        this.exit();
                        break;
                    default:
                        System.out.println("Invalid selection.");
                        break;
            }
        }
    }
    public void exit()                             
    {
        System.out.println("Exiting");
        start = false;
        System.exit(0);
    }
    public void addMovie()
    {        
       System.out.println("Insert Movie Name: ");
       title = input.nextLine();

       System.out.println("Insert Director Name: ");
       director = input.nextLine();     

        System.out.println("Insert Actor One Name: ");
         actOne = input.nextLine();

        System.out.println("Insert Actor Two Name: ");
        actTwo = input.nextLine();

        System.out.println("Insert Actor Three Name: ");
        actThree = input.nextLine();

        System.out.println("Insert Movie Rating: ");
        rating = input.nextInt();


         movie.add(new Movie(title, director, actOne, actTwo, actThree, rating));

    }



}

我只是想看看我的程序是否真的添加到数组列表中。 谢谢

【问题讨论】:

  • 覆盖 toString 并打印列表...
  • 正如声明中所说的ArrayList&lt;Movie&gt;,该列表仅包含Movie,而不是Stringint
  • 好的,那么您尝试做些什么来实现这一目标?它有什么问题?
  • "我只是想看看我的程序是否真的添加到数组列表中" - 您可以简单地在您的 addMovie() 方法的末尾添加一个断点并检查 movie 的内容使用IDE 的调试工具。

标签: java arraylist


【解决方案1】:

您可能会发现在电影类中重写 toString 方法很有帮助,然后在每次使用它时打印 arrayList...

System.out.println("actual state of the list: "+movie);

并覆盖电影类中的 toString 方法...描述对象的东西...例如:

@Override
public String toString() {
    return "Movie [rating=" + rating + ", title=" + title + ", directorName=" + directorName + ", actorOne="
            + actorOne + ", actorTwo=" + actorTwo + ", actorThree=" + actorThree + "]";
}

【讨论】:

    猜你喜欢
    • 2013-03-09
    • 2019-12-14
    • 2018-10-20
    • 1970-01-01
    • 1970-01-01
    • 2019-03-18
    • 2017-12-01
    • 1970-01-01
    • 2021-09-26
    相关资源
    最近更新 更多