即時編碼相當新穎。我有一系列電影。我試圖做的是刪除銷售數量最少的電影。在位於票房類的我的方法「卸妝」中,我收到一條錯誤消息,說明「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
儘管你一直在說'數組','movies'是一個['List'](http://docs.oracle.com/javase/8/docs/api/java/util/List.html)。 – Powerlord 2014-09-10 19:17:58
its = new ArrayList <>雖然,所以它不是一個數組? – 2014-09-10 19:29:26
雖然'ArrayList'確實使用數組作爲其後備存儲,但您不直接對其進行操作。對於這個問題,因爲'movies'被聲明爲'List'作爲它的類型,所以它可以創建爲'LinkedList '或'CopyOnWriteArrayList ',其他代碼將保持不變。這是因爲你是針對'List'接口進行編程的,而不是'ArrayList'。 –
Powerlord
2014-09-10 19:31:48