传递ArrayList并返回输出值

时间:2013-10-22 17:28:41

标签: java arraylist

我一直试图将arraylist传递到我的租赁菜单类并提取相关的电影标题,id等但我不断收到不兼容类型的错误。有人可以告诉我我的概念我错了吗?

import java.util.ArrayList;
public class DVDManager {
    private ArrayList<DVD> group;
    private int Id;
    private String title;
    private char genre;

public DVDManager (){
    this.group = new ArrayList<DVD>();
}

public DVDManager (int Id, String title, char genre){
    this.Id = Id;
    this.title = title;
    this.genre = genre;
    this.group = new ArrayList<DVD>();
}
public int getId (){
    return Id;
}

public String getTitle(){
    return title;
}

public char getGenre(){
    return genre;
}

public void addDVD (int Id, String title, char genre){
    Id = group.size()+1;
    group.add(new DVD(Id, title, genre));
}

public ArrayList<DVD> retrievalAll(){  
    return group;
}
}

import java.util.*;
public class RentalMenu{
    private DVDManager dvdManager;

public RentalMenu(){
    dvdManager = new DVDManager();
}

public void processListAllDVDs(){

    DVDManager m1 = new DVDManager();

    //**ERROR IS THE LINE BELOW!!!!!!!!!!!!!**

    ArrayList<DVD> myList = m1.retrievalAll();
    //listDVDs = dvdManager.retrievalAll();
    for (int i=0; i<myList.size(); i++){

        DVDManager listDVDs = myList.get(i);
        System.out.println(listDVDs.getTitle + listDVDs.getGenre);
    }
}

public void readOption(){
    System.out.println("*=======================*");
    System.out.println("* Choose the following*  ");
    System.out.println("1) Add a new DVD         ");
    System.out.println("2) search a DVD         ");
    System.out.println("3) Rent a DVD         ");
    System.out.println("4) Show all DVD       ");
    System.out.println("5) EXIT       ");
    Scanner sc = new Scanner(System.in);

    int option = sc.nextInt();
    int movId = 1;

        if (option == 1){ 
            System.out.println("Enter movie title");
            sc.nextLine();
            String newMovie = sc.nextLine();
            System.out.println("Enter genre");
            char g = sc.nextLine().charAt(0);
            dvdManager.addDVD(movId, newMovie, g);
            processListAllDVDs();
        }

}
}

1 个答案:

答案 0 :(得分:1)

   for (int i=0; i<myList.size(); i++){
        DVDManager listDVDs = myList.get(i);      //here is the problem
        System.out.println(listDVDs.getTitle + listDVDs.getGenre);
   }

您需要更改为

  DVD dvd= myList.get(i);