英文:
How to return properly value in Java
问题
import java.util.*;
class Book {
public String title;
public int year;
public Book(String title, int year) {
this.title = title;
this.year = year;
}
public String getTitle() {
return title;
}
public int getYear() {
return year;
}
}
class MyLinkedList {
public List<Book> getTitleBook(List<Book> allBooks, int maxYear) {
LinkedList<Book> book = new LinkedList<>();
book.add(new Book("Ogniem i Mieczem", 1884));
book.add(new Book("Krzyżacy", 1897));
book.add(new Book("Thinking in Java", 1998));
book.add(new Book("Czysty kod", 2008));
List<Book> result = new ArrayList<>(); // Create a list to store books
for (Book myBook : book) {
if (myBook.getYear() <= maxYear)
result.add(myBook); // Add books released under the specified year to the result list
}
return result; // Return the list of books
}
}
英文:
I have linkedlist and i want get value of all my book which is relased under 1998 year. Java compiler inform me: " missing return statement"
Here is my scratch of code:
class Book {
public String title;
public int year;
public Book(String title, int year) {
this.title = title;
this.year = year;
}
public String getTitle() {
return title;
}
public int getYear() {
return year;
}
}
class MyLinkedList {
public List < Book > getTitleBook(List < Book > allBooks, int maxYear) {
LinkedList < Book > book = new LinkedList < > ();
book.add(new Book("Ogniem i Mieczem", 1884));
book.add(new Book("Krzyżacy", 1897));
book.add(new Book("Thinking in Java", 1998));
book.add(new Book("Czysty kod", 2008));
for (Book myBook: book) {
if (myBook.getYear() <= maxYear)
System.out.println("Nazwa książki " + myBook.getTitle());
}
}
}
Thank you for your help
答案1
得分: 0
你想要将书添加到你的ArrayList并返回。
另外,我不确定为什么存在"allBooks"参数,以及为什么你要手动将Book对象添加到LinkedList中。
public ArrayList<Book> getTitleBook(List<Book> allBooks, int maxYear) {
LinkedList<Book> book = new LinkedList<Book>();
book.add(new Book("Ogniem i Mieczem", 1884));
book.add(new Book("Krzyżacy", 1897));
book.add(new Book("Thinking in Java", 1998));
book.add(new Book("Czysty kod", 2008));
ArrayList<Book> returnBooks = new ArrayList<Book>();
for (Book myBook : book) {
if (myBook.getYear() <= maxYear) {
System.out.println("Nazwa książki " + myBook.getTitle());
returnBooks.add(myBook);
}
}
return returnBooks;
}
英文:
You want to add to your ArrayList and return that.
Also, I'm unsure as to why the "allBooks" parameter exists, and why you're manually adding the Book objects to the LinkedList.
public ArrayList<Book> getTitleBook(List<Book> allBooks, int maxYear) {
LinkedList<Book> book = new LinkedList<Book>();
book.add(new Book("Ogniem i Mieczem", 1884));
book.add(new Book("Krzyżacy", 1897));
book.add(new Book("Thinking in Java", 1998));
book.add(new Book("Czysty kod", 2008));
ArrayList<Book> returnBooks = new ArrayList<Book>();
for (Book myBook: book) {
if (myBook.getYear() <= maxYear) {
System.out.println("Nazwa książki " + myBook.getTitle());
returnBooks.add(myBook);
}
}
return returnBooks;
}
专注分享java语言的经验与见解,让所有开发者获益!
评论