【发布时间】:2015-07-05 07:28:14
【问题描述】:
晚安。我正在尝试从 ArrayList 中检索和比较一个 int 变量值(如果可能的话),但无论我做什么,它都不会起作用。我已经尝试过 contains()、get() 等方法。我想我的逻辑真的很糟糕,有人可以帮助我吗?请问?
public class Obras extends Books implements ILibrary {
protected ArrayList<Obras> ListObra = new ArrayList<Obras>();
protected String typeObra;
protected String statusBorrow;
protected int ISBNuser;
Scanner userInput = new Scanner(System.in);
Scanner tipoInput = new Scanner(System.in);
public void createnewObra()
{
System.out.println("Insert the type of the new item: [Book, article...");
typeObra = tipoInput.nextLine();
super.createnewObra();
}
....
public void addObra() {
Obras newObra = new Obras();
newObra.createnewObra();
ListObra.add(newObra);
System.out.println("> Uma nova obra foi adicionada com sucesso!\n");
....
public void BorrowObra() {
System.out.println("> Choose a book from the list: ");
showListObras();
System.out.println("\n\n> Please choose one of the items from the above list by typing it's ISBN value: ");
ISBNuser = opcaoInput.nextInt();.
if(ListObra.get(ISBN).equals(ISBNuser))
{
System.out.println("> You successfully borrowed this book");
statusBorrow = false;
}
【问题讨论】:
-
我认为他在
Obras中有overrideequals函数,@sparkss 你能粘贴你的equals函数吗? -
ISBN来自哪里? -
请隔离程序中围绕问题的几行代码(即涉及读取 ArrayLists 的代码),然后仅发布。另外,请更清楚到底发生了什么问题。有错误信息吗?您是否在为它的概念方面而苦苦挣扎?
-
@DanielNugent - 嘿,这是 Obras 扩展的类中的一个变量。
-
@chengpohi - if(ListObra.get(ISBN).equals(ISBNuser)) ISBN 是另一个类的整数变量,由用户在 createnewObra() 中定义。
标签: java list arraylist int compare