In dem folgenden Code möchte ich indexOf()
verwenden, aber ich kann den richtigen Weg nicht finden.Verwenden Sie indexOf mit einer ArrayList, die mehrere Objekte hat
Filmklasse:
public class Movie {
private String title;
private String genre;
private String actor;
private int yearPublished;
public Movie(String title, String genre, String actor, int yearPublished) {
this.title = title;
this.genre = genre;
this.actor = actor;
this.yearPublished = yearPublished;
}
@Override
public String toString() {
return title + ", Genre: " + genre + ", Actor(s):" + actor + ", Year of publishing: " + yearPublished;
}
public String getTitle() {
return title;
}
public String getGenre() {
return genre;
}
public String getActor() {
return actor;
}
public int getYearPublished() {
return yearPublished;
}
}
Controller-Klasse:
public class Controller {
void start() {
scanning();
printing("Movies:");
selectYourMovie();
}
private List<Movie> movies = new ArrayList<>();
private void scanning() {
try {
Scanner fileScanner = new Scanner(new File("movies.txt"));
String row;
String []data;
while (fileScanner.hasNextLine()) {
row = fileScanner.nextLine();
data = row.split(";");
movies.add(new Movie(data[0], data[1], data[2], Integer.parseInt(data[3])));
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void printing(String cim) {
for (Movie movie : movies) {
System.out.println(movie);
}
}
private void selectYourMovie() {
System.out.println(movies.indexOf(/*What to put here?*/);
}
}
Und der Inhalt der txt-Datei:
The Matrix;action;Keanu Reeves;1999
The Lord of the Rings;fantasy;Elijah Wood;2001
Harry Potter;fantasy;Daniel Radcliffe;2001
The Notebook;drama;Ryan Gosling;2004
Step Up;drama, dance;Channing Tatum;2006
Pulp Fiction;crime;Samuel L. Jackson, John Travolta;1994
Star Wars: A New Hope;action;Mark Hamill;1977
So würde ich den Index des gegebenen zurückkommen Film, aber ich konnte nicht finden, wie man es mit einer Liste tut, die mehrere Gegenstände hat. Da passiert eine ganze Zeile des Txt nicht.
Hinweise dazu?