如何删除字符串数组中的元素

时间:2014-09-10 19:14:34

标签: java string

我在编码时相当新。我有一系列电影。我想要做的是删除销售数量最少的电影。在我的方法'卸妆'位于票房类,我得到一个错误说“int不能被解除引用”在该行包括此代码“((movies.get(i).sales).equals(small))”。我也调用了remove方法将其从数组中删除但我猜测删除在java中是不合法的。我需要做什么调用才能将其从数组中删除并更新长度。

import java.util.*;
public class LIANGLAB1p2
{
    public static void main(String[] argv)
    {
        BoxOffice bo = new BoxOffice();
        bo.add(new Movie("starboat","pg","action"));
        bo.add(new Movie("bloody banquet","pg-13","horror"));
        bo.add(new Movie("godizilla eats tokyo","pg","horror"));
        bo.add(new Movie("geeks in love","pg","comedy"));
        bo.add(new Movie("bad cop, worse cop","r","comedy"));
        bo.add(new Movie("lost of bullets","r","action"));
        bo.add(new Movie("the eliminator","r","action"));
        bo.add(new Movie("the garbage collector","pg","comedy"));
        bo.add(new Movie("the dentist","r","comedy"));
        bo.add(new Movie("the professor","r","horror"));
        bo.add(new Movie("bloody noon", "r", "action"));

        for(int i=0;i<200;i++) bo.sellticket("the professor");
        for(int i=0;i<100;i++) bo.sellticket("starboat");
        for(int i=0;i<120;i++) bo.sellticket("the eliminator");
        for(int i=0;i<10;i++) bo.sellticket("bloody banquet");
        for(int i=0;i<40;i++) bo.sellticket("the dentist");
        for(int i=0;i<25;i++) bo.sellticket("geeks in love");
        bo.listmovie();
        bo.genrecounter();
        System.out.println("The most popular genre is " +bo.genrecounter());
        System.out.println("The most popular movie is " +bo.mostpopular());



        System.out.println("The sales have been reset to 0");
        bo.reset();


    }
}
class Movie
{
    public int sales;
    public int genr;
    public String title;
    public String rating;
    public String genre;

    public Movie (String t, String r, String g){
        title = t; rating = r; genre = g; sales = 0; genr = 0;
    }
    public String toString(){
        return title+ " - rated " +rating+" - genre: "+genre;
    }
}

class BoxOffice{

    List<Movie> movies = new ArrayList<>();

    public double ticketprice;
    public void changeprice(double newprice){       
        ticketprice = newprice;
    }//changeprice

    public BoxOffice(){                 // constructor
        ticketprice = 10.00;
    }//BoxOffice

    public void add(Movie m){           //adds movie
        movies.add(m);
    }//add

    public void listmovie(){            //prints list of all movies
        for (int i=0; i<movies.size(); i++)
        {
            System.out.println(movies.get(i).toString());
        }
    }//listmovie

    public int get(String t){           //gets movie by title
        for (int i=0; i<movies.size(); i++)
            if (t.equals(movies.get(i).title)){
                return i;
            }
            return -1;
    }//get

    public void sellticket(String m){   
        int i = get(m);
        if (i>=0)
            movies.get(i).sales +=1;
        else
            System.out.println("that movie is currently not showing");
    }//sellticket

    public String mostpopular() {   //returns name of most popular movies
        String ax = "";
        int bx = -1;
        for (int i=0;i<movies.size();i++)
            if (movies.get(i).sales>bx)
            {
                ax = movies.get(i).title;
                bx = movies.get(i).sales;
            }
        return ax;
    }//mostpopuler

    public void reset(){
        for (int i=0;i<movies.size();i++){
            movies.get(i).sales = 0;
        }
    }

    public void remover(){
        int small = movies.get(0).sales;
        for (int i=0;i<movies.size();i++){
            if (movies.get(i).sales < small)
                small = movies.get(i).sales;
            }
        for (int i=0;i<movies.size();i++){
            if ((movies.get(i).sales).equals(small))
                remove(movies.get(i));
        }
    }

    public String genrecounter(){   //returns most populer genre
        int horror = 0;
        int action = 0;
        int comedy = 0;
        for (int i=0;i<movies.size();i++)
            if ((movies.get(i).genre).equals("horror")){
                horror++;
            }
            else if((movies.get(i).genre).equals("comedy")){
                comedy++;
            }
            else {
                action++;
            }
        if (horror > action || horror > comedy){
            return "horror";
        }
        else if (action > comedy || action > horror){
            return "action";
        }
        else {
            return "comedy";
        }
    }


}//BoxOffice

2 个答案:

答案 0 :(得分:2)

如果movies.get(i).sales返回int,则不能在其上调用.equals(或任何实例方法),因为int是基本类型。请改用movies.get(i).sales == small

至于从ArrayList中删除一个项目,可以使用movies.remove(i)来完成,但请注意,作为副作用,这将改变列表中所有元素的索引,其中索引为&gt;我,所以如果你在删除后继续迭代列表,你应该再次处理第i个元素。

因此,您应该将remover内的循环更改为:

    for (int i=0;i<movies.size();i++){
        if (movies.get(i).sales==small) {
            movies.remove(i);
            i--;
        }
    }

答案 1 :(得分:0)

java List确实有一个remove方法,您可以在其中指定要删除的索引或对象。因此,您的代码可以简化为:

public void remover() {
    Movie smallest = movies.get(0);
    for (Movie movie:movies) {
        if (movie.sales < smallest.sales) {
            smallest = movie;
        }
    }
    movies.remove(smallest);
} 

一个简单的解释: 1)获取第一部电影,最初将其设置为销售数量最少的电影 2)循环所有电影,检查每个电影是否低于目前的最低价格 3)如果它们较低,则更新对销售数量最少的电影的引用 4)最后,通过对象引用

删除列表中销售数量最少的电影