2017-05-08 129 views
0

我很好奇如何在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)); 

    } 



} 

我只想看看我我的程序实际上是增加了数组列表。 谢谢

+2

覆盖的toString并打印列表... –

+0

作为声明说,ArrayList的'',列表中只包含'Movie',不'String'和'INT '。 – AxelH

+0

好的,那么你是怎么做到的呢?它有什么问题? –

回答

0

您可能会发现在电影类有益的压倒一切的toString方法,然后打印你用它做什么ArrayList的每次...

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

,并覆盖在电影类的toString方法.. 。财产以后descripting对象......像:

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