【问题标题】:Retrive and compare int value from an ArrayList从 ArrayList 中检索和比较 int 值
【发布时间】: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 中有override equals 函数,@sparkss 你能粘贴你的equals 函数吗?
  • ISBN 来自哪里?
  • 请隔离程序中围绕问题的几行代码(即涉及读取 ArrayLists 的代码),然后仅发布。另外,请更清楚到底发生了什么问题。有错误信息吗?您是否在为它的概念方面而苦苦挣扎?
  • @DanielNugent - 嘿,这是 Obras 扩展的类中的一个变量。
  • @chengpohi - if(ListObra.get(ISBN).equals(ISBNuser)) ISBN 是另一个类的整数变量,由用户在 createnewObra() 中定义。

标签: java list arraylist int compare


【解决方案1】:

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

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

List<Integer> listOfIntegers = new ArrayList<Integer>();

您的列表似乎包含 Obras 类的对象。

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

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

【讨论】:

  • 哦,谢谢,是的,我现在注意到了。 ISBN 是一个接收用户输入的整数变量,它与其他不同类型的变量一起添加到 ListObras。我想那是我的错误。是的,对 Java 命名标准感到抱歉。
【解决方案2】:
ListObra.get(ISBN).equals(ISBNuser)

到:

Obras o = ListObra.get(ISBN);
if (o != null && o.getISBNuser() == ISBNuser)  {
    System.out.println("> You successfully borrowed this book");
    statusBorrow = false;
}

因为你只得到了一个对象Obras而你没有OverrideequalObras中起作用,所以你需要得到Integer ISBUser并且等于用户输入。

另一种方式:

Override equal:

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)

【讨论】:

  • 是的,这就是我想要做的@chengpohi。关于您的代码,所以对于我所看到的,我将不得不为 ISBNuser 创建一个 get() 方法? ISBN 和 ISBNuser 都是通过用户输入获取值的变量,我想比较这两个值。
  • 我想你也可以试试第二种方法。
猜你喜欢
  • 1970-01-01
  • 2017-06-25
  • 2021-06-23
  • 2018-03-02
  • 2013-11-01
  • 2023-04-08
  • 2011-12-16
  • 2011-07-13
  • 1970-01-01
相关资源
最近更新 更多