您好我正在尝试制作电影数据库。决定使用arraylist因为我们不能使用数组。它将有4个类,Movie,MovieDatabase,Playlist和Interface。我希望将Movie类中的Movie对象添加到Moviedatabase中的Arraylist中,然后可以在界面中添加和删除它们。我收到很多错误,说无法找到变量。我使用另一个类似的问题作为模板,无法弄清楚为什么它不会编译。 这是我的电影课
public class Movie
{
private String name, director;
private int fileSize, duration;
public Movie()
{
name = "";
director = "";
fileSize = 0;
duration = 0;
}
Movie(String newName, String newDirector, int newfileSize, int newDuration)
{
this.name = newName;
this.director = newDirector;
this.fileSize = newfileSize;
this.duration = newDuration;
}
public void setName(String newName)
{
name = newName;
}
public void setDirector(String newDirector)
{
director = newDirector;
}
public void setfileSize(int newfileSize)
{
fileSize = newfileSize;
}
public void setDuration(int newDuration)
{
duration = newDuration;
}
public String getName()
{
return name;
}
public String getDirector()
{
return director;
}
public int getfileSize()
{
return fileSize;
}
public int getDuration()
{
return duration;
}
}
和MovieDatabase类
import java.util.*;
public class MovieDatabase
{ //start class
private ArrayList<Movie> list = new ArrayList<Movie>(4); //creates ArrayList
Movie newMovie = new Movie(newName, newDirector, newfileSize, newDuration); //instantiates Movie class
public void add(Movie newMovie) //method adds a new Movie object to the array list
{ //start add
list.add(newMovie);
} //end add
public void add(String newName, String newDirector, int newfileSize, int newDuration) //accepts parameters from main method to add to new object
{ //start add
newName = name;
newDirector = director;
newfileSize = fileSize;
newDuration = duration;
newMovie.setName(newName);
newMovie.setDirector(newDirector);
newMovie.setfileSize(newfileSize);
newMovie.setDuration(newDuration);
list.add(newName,newDirector,newfileSize,newDuration);
} //end add
}
//end class
编辑:错误消息
MovieDatabase.java:6: error: cannot find symbol
Movie newMovie = new Movie(newName, newDirector, newfileSize, newDuration); //instantiates Movie class
^
symbol: variable newName
location: class MovieDatabase
MovieDatabase.java:6: error: cannot find symbol
Movie newMovie = new Movie(newName, newDirector, newfileSize, newDuration); //instantiates Movie class
^
symbol: variable newDirector
location: class MovieDatabase
MovieDatabase.java:6: error: cannot find symbol
Movie newMovie = new Movie(newName, newDirector, newfileSize, newDuration); //instantiates Movie class
^
symbol: variable newfileSize
location: class MovieDatabase
MovieDatabase.java:6: error: cannot find symbol
Movie newMovie = new Movie(newName, newDirector, newfileSize, newDuration); //instantiates Movie class
^
symbol: variable newDuration
location: class MovieDatabase
MovieDatabase.java:17: error: cannot find symbol
newName = name;
^
symbol: variable name
location: class MovieDatabase
MovieDatabase.java:18: error: cannot find symbol
newDirector = director;
^
symbol: variable director
location: class MovieDatabase
MovieDatabase.java:19: error: cannot find symbol
newfileSize = fileSize;
^
symbol: variable fileSize
location: class MovieDatabase
MovieDatabase.java:20: error: cannot find symbol
newDuration = duration;
^
symbol: variable duration
location: class MovieDatabase
MovieDatabase.java:27: error: no suitable method found for add(String,String,int,int)
list.add(newName,newDirector,newfileSize,newDuration);
^
method Collection.add(Movie) is not applicable
(actual and formal argument lists differ in length)
method List.add(Movie) is not applicable
(actual and formal argument lists differ in length)
method List.add(int,Movie) is not applicable
(actual and formal argument lists differ in length)
method AbstractCollection.add(Movie) is not applicable
(actual and formal argument lists differ in length)
method AbstractList.add(Movie) is not applicable
(actual and formal argument lists differ in length)
method AbstractList.add(int,Movie) is not applicable
(actual and formal argument lists differ in length)
method ArrayList.add(Movie) is not applicable
(actual and formal argument lists differ in length)
method ArrayList.add(int,Movie) is not applicable
(actual and formal argument lists differ in length)
答案 0 :(得分:0)
Movie newMovie = new Movie(newName, newDirector, newfileSize, newDuration);
您需要定义newName
,newDirector
,newfileSize
和newDuration
,因为这些变量目前不存在于MovieDatabase
。< / p>
答案 1 :(得分:0)
您不需要将数组列表的长度指定为4. ArrayList的用途是具有列表属性的数组性能,例如按名称或位置添加和删除项目,大小检查,等。
此外,方法add(Movie newMvoie)将影片添加到类变量列表中,但您尝试在类中创建列表。实际上,您应该在没有新构造函数的情况下声明列表成员变量。
public class MovieDataBase {
private ArrayList<Movie> list;
}
您不能只在方法或构造函数之外的类中编写代码。您需要创建一个MovieDatabase构造函数,并可能有另一个类来驱动该应用程序。
答案 2 :(得分:0)
以下代码使用的是未在 MovieDatabase 类
中声明的变量public void add(String newName, String newDirector, int newfileSize, int newDuration) //accepts parameters from main method to add to new object
{ //start add
newName = name;
newDirector = director;
newfileSize = fileSize;
newDuration = duration;
newMovie.setName(newName);
newMovie.setDirector(newDirector);
newMovie.setfileSize(newfileSize);
newMovie.setDuration(newDuration);
list.add(newName,newDirector,newfileSize,newDuration);
}
你的意思是:
public void add(String newName, String newDirector, int newfileSize, int newDuration) //accepts parameters from main method to add to new object
{
list.add(new Movie(newName,newDirector,newfileSize,newDuration));
}