2014-09-10 85 views
-1

即時編碼相當新穎。我有一系列電影。我試圖做的是刪除銷售數量最少的電影。在位於票房類的我的方法「卸妝」中,我收到一條錯誤消息,說明「int can not be dereferenced」,包括此代碼「((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 
+0

儘管你一直在說'數組','movies'是一個['List'](http://docs.oracle.com/javase/8/docs/api/java/util/List.html)。 – Powerlord 2014-09-10 19:17:58

+0

its = new ArrayList <>雖然,所以它不是一個數組? – 2014-09-10 19:29:26

+0

雖然'ArrayList'確實使用數組作爲其後備存儲,但您不直接對其進行操作。對於這個問題,因爲'movies'被聲明爲'List '作爲它的類型,所以它可以創建爲'LinkedList '或'CopyOnWriteArrayList ',其他代碼將保持不變。這是因爲你是針對'List'接口進行編程的,而不是'ArrayList'。 – Powerlord 2014-09-10 19:31:48

回答

2

如果movies.get(i).sales返回int,則不能在其上調用.equals(或任何實例方法)中,由於int是基本類型。改爲使用movies.get(i).sales == small

至於從ArrayList中刪除一個項目,你可以使用movies.remove(i)來做到這一點,但是請注意,作爲副作用,這將改變列表中所有索引> i的元素的索引,所以如果你繼續迭代在刪除之後在列表中,您應該再次處理第i個元素。

因此,應改變環路內remover到:

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

一個java List確實有一個刪除的方法,其中,可以指定所述索引或對象以移除。所以,你的代碼可以簡化爲:

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)最後,通過對象參考

從列表中刪除銷售量最低的電影
+0

你能解釋這段代碼是如何工作的嗎?int min = Integer.MAX_VALUE; – 2014-09-10 19:25:16

+0

從那時起,我改變了它,但'int min = Integer.MAX_VALUE'只是將'min'變量設置爲一個很大的數值,以允許'sales'變小。如果'min'爲'0',那麼沒有什麼會更小(除非你有負面的銷售) – DairyLea 2014-09-10 19:28:53

+0

好的謝謝,我明白了。 – 2014-09-10 19:32:17

相關問題