晚安。我正在尝试从 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;
}
最佳答案
要从 ArrayList
获取 int,您的 ArrayList
必须按照以下方式定义:
ArrayList<Integer> arrayListOfIntegers = new ArrayList<Integer>();
或
List<Integer> listOfIntegers = new ArrayList<Integer>();
您的列表似乎包含 Obras
类的对象。
此外,当您调用 ListObra.get(ISBN)
时,此方法旨在返回列表中指定索引处的对象 - 我怀疑 ISBN 不是列表中的索引,而是一本书的 ISBN?
另外,请尝试遵守 Java 命名标准 - 变量以小写字母开头,方法使用驼峰式大小写(例如 createNewObra()
)。它使其他开发人员更容易理解。
关于java - 从 ArrayList 中检索并比较 int 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29885664/