Java 从ArrayList中检索并比较int值

Java 从ArrayList中检索并比较int值,java,list,arraylist,int,compare,Java,List,Arraylist,Int,Compare,晚安。我试图从ArrayList中检索和比较一个int变量值(如果可能的话),但无论我做什么,它都不会起作用。我已经尝试过contains()、get()等方法。我想我的逻辑真的很糟糕,有人能帮我吗?求你了 public class Obras extends Books implements ILibrary { protected ArrayList<Obras> ListObra = new ArrayList<Obras>();

晚安。我试图从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;
        }
public类Obras扩展了ILibrary{
受保护的ArrayList ListObra=新ArrayList();
保护字符串typeObra;
受保护的字符串状态;
受保护的用户;
扫描仪用户输入=新扫描仪(System.in);
扫描仪tipoInput=新扫描仪(System.in);
public void createnewObra()
{
System.out.println(“插入新项目的类型:[书籍,文章…);
typeObra=tipoInput.nextLine();
super.createnewObra();
}
....
公共无效addObra(){
Obras newObra=新Obras();
newObra.createnewObra();
添加(newObra);
System.out.println(“>Uma nova obra foi adicionada com successo!\n”);
....
公共图书馆{
System.out.println(“>从列表中选择一本书:”);
showListObras();
System.out.println(“\n\n>请键入上面列表中的ISBN值:”,从中选择一项;
ISBNuser=opcaoint.nextInt();。
if(ListObra.get(ISBN).equals(ISBNuser))
{
System.out.println(“>您成功地借用了这本书”);
statusBorrow=false;
}

要从
ArrayList
获取int,必须按照以下方式定义
ArrayList

ArrayList<Integer> arrayListOfIntegers = new ArrayList<Integer>();
ArrayList ArrayListFintegers=new ArrayList();

List-listOfIntegers=new-ArrayList();
您的列表似乎包含类对象
Obras

另外,当您调用
ListObra.get(ISBN)
时,此方法旨在返回列表中指定索引处的对象-我怀疑ISBN不是列表中的索引,而是书籍的ISBN

另一方面,请尝试遵守Java命名标准-变量以小写字母开头,方法使用驼峰大小写(例如
createNewObra()
),这使其他开发人员更容易理解

ListObra.get(ISBN).equals(ISBNuser)
致:

因为你只得到一个对象
Obras
,而没有
覆盖
Obras
中的
equal
函数,所以你需要得到
整数ISBUser
并等于用户输入

另一种方式:

覆盖相等值

public class Obras extends Books implements ILibrary {

@Override
public boolean equals(Object e) {
   Integer i = (Integer)e;
   if (this.ISBUser == i) return true;
   return false;
}
}
因此,现在可以使用
equals
函数来比较:

ListObra.get(ISBN).equals(ISBNuser)

我想他在
Obras
中有
override
equals
函数,@sparkss你能粘贴你的
equals
函数吗?
ISBN
来自哪里?请隔离你程序中围绕这个问题的几行代码(即,涉及读取数组列表的代码),并且只发布它。此外,请更清楚地说明到底发生了什么问题。是否有错误消息?您是否正在与概念方面进行斗争?@DanielNugent-嘿,这是Obras扩展的类中的一个变量。@chengpohi-if(ListObra.get(ISBN.equals)(ISBNuser))ISBN是来自另一个类的整数变量,由用户在createnewObra()中定义.噢,谢谢,是的,我注意到了。ISBN是一个整数变量,它接收用户输入,并用其他不同类型的变量添加到ListObras中。我想这是我的错误。是的,对Java命名标准感到抱歉。是的,这就是我试图做的@chengpohi。关于你的代码,我看到的是,我必须创建一个get()ISBNuser的方法?ISBN和ISBNuser都是通过用户输入获取其值的变量,我想比较这两个值。我想您也可以尝试第二种方法。
public class Obras extends Books implements ILibrary {

@Override
public boolean equals(Object e) {
   Integer i = (Integer)e;
   if (this.ISBUser == i) return true;
   return false;
}
}
ListObra.get(ISBN).equals(ISBNuser)