java - How to remove an element in an array of strings -
im new @ coding. have array of movies. im trying delete movie least number of sales. in method 'remover' located in box office class, error saying " int cannot dereferenced" on line including code "((movies.get(i).sales).equals(small))". called remove method delete array im guessing remove isnt legitimate in java. call have make remove array , update length.
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 popular genre " +bo.genrecounter()); system.out.println("the popular movie " +bo.mostpopular()); system.out.println("the sales have been reset 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 movies (int i=0; i<movies.size(); i++) { system.out.println(movies.get(i).tostring()); } }//listmovie public int get(string t){ //gets movie title (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 = get(m); if (i>=0) movies.get(i).sales +=1; else system.out.println("that movie not showing"); }//sellticket public string mostpopular() { //returns name of popular movies string ax = ""; int bx = -1; (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(){ (int i=0;i<movies.size();i++){ movies.get(i).sales = 0; } } public void remover(){ int small = movies.get(0).sales; (int i=0;i<movies.size();i++){ if (movies.get(i).sales < small) small = movies.get(i).sales; } (int i=0;i<movies.size();i++){ if ((movies.get(i).sales).equals(small)) remove(movies.get(i)); } } public string genrecounter(){ //returns populer genre int horror = 0; int action = 0; int comedy = 0; (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
if movies.get(i).sales
returns int, can't call .equals
(or instance method) on it, since int
primitive type. use movies.get(i).sales == small
instead.
as removing item arraylist, can movies.remove(i)
, note side effect, change index of elements in list index > i, if continue iterating on list after removal, should process i'th element again.
therefore, should change loop inside remover
:
(int i=0;i<movies.size();i++){ if (movies.get(i).sales==small) { movies.remove(i); i--; } }
Comments
Post a Comment