即时编码相当新颖。我有一系列电影。我试图做的是删除销售数量最少的电影。在位于票房类的我的方法“卸妆”中,我收到一条错误消息,说明“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