2017-05-08 130 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 + "]"; 
}