아래 코드에서 indexOf()
을 사용하고 싶지만 적절한 방법을 찾을 수 없습니다.여러 객체가있는 ArrayList에 indexOf를 사용하십시오.
영화 클래스 :
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;
}
}
컨트롤러 클래스 :
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
그래서 나는 주어진의 인덱스를 반환하고 싶습니다 :
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?*/);
}
}
그리고 txt 파일의 내용 영화,하지만 난 여러 개체를 가진 목록으로 그것을하는 방법을 찾을 수 없습니다. txt의 전체 행을 전달하면 작동하지 않습니다.
어떤 힌트가 있습니까?