如何从ArrayLists中删除对象

时间:2015-04-30 00:41:25

标签: java arraylist

所以我有这个功课,创建一个java电影节目。它应该有添加电影(标题,演员和出场日期),显示(添加所有电影)和删除电影(按电影标题)选项。 到目前为止,我能够创建addMovie()和showMovie()方法...但我真的卡住了广告removeMovies()。

以下是Main.java的代码:

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    static Scanner input = new Scanner(System.in);
    static ArrayList<Movies> movHolder = new ArrayList<Movies>();

    public static void main(String[] args) {    
        int op = -1;        
        while (op != 0){
            op= menuOption();
            switch(op){
            case 1:
                addMovies();
                break;
            case 2:
                removeMovies();
                break;
            case 3:
                showMovies();               
                break;
            case 0:
                System.out.print("\n\nYou have exited the program!");
                break;
            default:
                System.out.println("\nWrong input!");
            }           
        }
    }

    public static int menuOption(){
        int option;
        System.out.println("\nMenu\n");
        System.out.println("1. Add new movies");
        System.out.println("2. Remove movies");
        System.out.println("3. Show all movies");
        System.out.println("0. Exit program");
        System.out.print("\nChoose an option: ");
        option = input.nextInt();

        return option;      
    }

    public static void addMovies(){
        String t, a, d;

        input.nextLine();
        System.out.println("\n---Adding movies---\n");
        System.out.print("Enter title of movie: ");
        t = input.nextLine();
        System.out.print("Enter actor's name: ");
        a = input.nextLine();
        System.out.print("Enter date of apearance: ");
        d = input.nextLine();

        Movies mov = new Movies(t, a, d);
        movHolder.add(mov);         
    }

    public static void removeMovies(){
        int choice;
        System.out.println("\n---Removing movies by title---\n");
        for(int i = 0; i < movHolder.size(); i++){
            System.out.println((i+1)+ ".) "+ movHolder.get(i).toString());
        }
        System.out.print("Enter movie do you want to remove?");
        choice = input.nextInt();           

    }

    public static void showMovies(){
        System.out.print("---Showing movie list---\n");
        for(int i = 0; i < movHolder.size(); i++){
            System.out.println((i+1)+ ".) "+ movHolder.get(i).toString());
        }       
    }   
}

这是带有Movie类的Movies.java:

public class Movies {

    private String title;
    private String actor;
    private String date;

    public Movies (String t, String a, String d){
        title = t;
        actor = a;
        date = d;
    }
    public Movies(){
        title = "";
        actor = "";
        date = "";
    }

    public String getTitle(){
        return title;
    }
    public String getActor(){
        return actor;
    }
    public String getDate(){
        return date;
    }
    public String toString(){
        return "\nTitle: " + title + 
               "\nActor: " + actor + 
               "\nRelease date: " + date;
    }   

}

你可能已经看到了,我是一个非常初学的java程序员。 请,如果有人可以帮助removeMovie()方法,我将非常感激。

2 个答案:

答案 0 :(得分:1)

由于您拥有应删除的电影的索引(choice - 1),因此您可以使用ArrayList.remove(int)

    System.out.print("Enter movie do you want to remove?");
    choice = input.nextInt();
    movHolder.remove(choice-1);

答案 1 :(得分:1)

您可以使用remove(int index)方法:

public static void removeMovies(){
     int choice;
     System.out.println("\n---Removing movies by title---\n");
     for(int i = 0; i < movHolder.size(); i++){
         System.out.println((i+1)+ ".) "+ movHolder.get(i).toString());
     }
     System.out.print("Enter movie do you want to remove?");
     choice = input.nextInt();

     // Decrement the index because you're asking the user for a 1 based input.    
     movHolder.remove(choice - 1)
}       

}