下面是我的 updateMovie 代码。我想问一下,在更新该特定条目中的值后,如何将 ArrayList 解析回对象。或者,是否有任何其他实现,我可以根据下面的代码更新 .txt 文件中的条目。
public static void updateMovie() throws IOException
{
Scanner sc = new Scanner(System.in);
ArrayList al = MovieDB.readMovies(filename) ;
ArrayList movie = new ArrayList();
int elementMovieID;
int confirmUpdate=0;
int index = 0;
String value;
Movie [] m2;
int MovieID;
String MovieName;
String MovieReleaseDate;
String MovieType;
String MovieRating;
String MovieStatus;
String MovieDuration;
try{
System.out.println("Enter MovieID to Update: ");
elementMovieID = sc.nextInt();
for (int i = 0 ; i < al.size() ; i++) {
Movie m1 = (Movie)al.get(i);
System.out.println("Movie ID: "+m1.getMovieID());
System.out.println("Movie Name: " + m1.getMovieName() );
System.out.println("Movie Duration: " + m1.getMovieType() );
if(elementMovieID==m1.getMovieID()){
movie.add(m1.getMovieID());
movie.add(m1.getMovieName());
movie.add(m1.getMovieReleaseDate());
movie.add(m1.getMovieType());
movie.add(m1.getMovieRating());
movie.add(m1.getMovieStatus());
movie.add(m1.getMovieDuration());
System.out.println("Enter the index to edit (1: Movie Name): ");
System.out.println("1: Movie Name: " + m1.getMovieName() );
System.out.println("2: Movie Release Date: " + m1.getMovieReleaseDate() );
System.out.println("3: Movie Type: " + m1.getMovieType() );
System.out.println("4: Movie Rating: " + m1.getMovieRating() );
System.out.println("5: Movie Status: " + m1.getMovieStatus() );
System.out.println("6: Movie Duration: " + m1.getMovieDuration() );
index = sc.nextInt();
for(int j=0; j<movie.size(); j++)
{
if(index==j){
System.out.println("Please Update: ");
value = sc.next();
movie.set(j, value);
System.out.println(movie);
}
}
}
}
MovieDB.saveMovies(filename, al);
}catch (IOException e) {
System.out.println("IOException > " + e.getMessage());
}
}