【问题标题】:Find index of objects in an array with same name, sort them by value, then change their values在具有相同名称的数组中查找对象的索引,按值对它们进行排序,然后更改它们的值
【发布时间】:2014-04-12 01:24:41
【问题描述】:

我真的被困在我的代码末尾,这是我迷路的地方。我的目标是从我创建的一个名为 Books 的类(未显示)中创建一个对象数组。它存储他们的 ISBN、标题和价格。该代码应该向用户询问书名和 ISBN #,如果它与数组中的任何书籍匹配,则所有具有相同 ISBN 和标题的书籍将从最低价格排序到最高价格,然后所有他们的价格将更改为最低价格的书的价格。我评论了我迷路的地方。非常感谢!

书籍类如下所示: 类书籍{ 私有字符串标题; 私人国际标准书号; 私人 int 价格;

        public Books(){
            title = "The Outsiders";
            ISBN = 1234;
            price = 14;
        }

        //regular constructor
        public Books(String T, int I, int P){
            title = T;
            ISBN = I;
            price = P;
        }
        //Copy Constructor
        public Books(Books aBook){
            this.title = aBook.title;
            this.ISBN = aBook.ISBN;
            this.price = aBook.price;
        }

这是我正在学习的课程的开始:

        //Beginning of ModifyBooks Class
    Books[] Library = new Books[10];

    Library[0] = new Books("blah", 1726374, 12.00);
    Library[1] = new Books("Lovely Bones", 111112, 20.00);
    Library[2] = new Books("Birds in a Fence", 111113, 13.00);
    Library[3] = new Books("Hunger Games", 111114, 14.50);
    Library[4] = new Books("Titanic", 738394, 12.5);
    Library[5] = new Books("Heroes", 7373849, 21.00);
    Library[6] = new Books(Library[1]);
    Library[7] = new Books(Library[1]);
    Library[8] = new Books(Library[2]);
    Library[9] = new Books(Library[3]);

    //Changing all prices of books
    for (int i = 0 ; i < Library.length ; i++){
        Library[i].price = i + 5;
    }

    //Keyboard configuration
    Scanner kb = new Scanner(System.in);

    System.out.println("Please enter a book's title:");
    String UserTitle = kb.nextLine();

    System.out.println("Please enter a book's ISBN Number:");
    int UserISBN = kb.nextInt();

    System.out.println("Your entered book's title is " + UserTitle + " and the ISBN is " + UserISBN);

    double[] sameBook = new double[10];
    int counter = 0;

这是我的代码没有做我想做的事情的地方,我不知道如何让它做我上面描述的但这是我的尝试。

    for (int i = 0 ; i < Library.length ; i++ ){
        if (UserTitle.equalsIgnoreCase(Library[i].title) && UserISBN == Library[i].ISBN){
            sameBook[i] = Library[i].price;
            counter++;
        } 
        else {
            sameBook[i] = 0;
        }
    }
    double[] SmallerLibrary = new double[counter];

    for (int i = 0 ; i < sameBook.length ; i++){
        if (sameBook[i] != 0){
            SmallerLibrary[i] = sameBook[i];
        }
    }

    Arrays.sort(SmallerLibrary);

}

}

【问题讨论】:

  • 最后一个 for 循环没有达到我想要的效果,但我不知道如何修复它。我更好地澄清了上面的问题。
  • 我假设您有两个不同的 Books 构造函数,一个接受 3 个参数,一个接受另一个 Books 对象的 1 个参数?
  • 您正在将 UserISBN(用户输入时以字符串形式出现)与数组的整数 ISBN 进行比较。在比较两个 ISBN 之前,您是否将用户输入转换为整数?

标签: java arrays sorting loops object


【解决方案1】:

考虑以下策略:

  1. 遍历数组中的每个书籍对象,并检查它是否与用户输入匹配。
  2. 如果这本书是匹配的,将它的索引存储在一个数组中以备后用,如果这本书的价格低于之前设置的价格,则更新全局最低价格变量的值(控制它的初始值)第一次通过案例)
  3. 完成对列表的迭代后,使用数组存储每本匹配书籍的索引,并使用全局最低价格变量更新它们的价格字段

【讨论】:

    【解决方案2】:

    问题是您正在迭代具有不同长度的数组,从而导致 ArrayIndexOutOfBounds。

    如果您输入“可爱的骨头”,111112,那么您将有 3 个匹配项。

    sameBook 长度始终为 10。

    double[] sameBook = new double[10];
    

    但 SmallerLibrary 长度为 3(用计数器初始化)

    double[] SmallerLibrary = new double[counter];
    

    因此,当迭代 sameBook.length 时,索引最终将大于 SmallerLibrary.length 导致异常。

    for (int i = 0 ; i < sameBook.length ; i++){
        if (sameBook[i] != 0){
            SmallerLibrary[i] = sameBook[i];  // Exception thrown here
        }
    }
    

    您需要为 SmallerLibrary 中的值创建一个单独的索引。

      for (int i = 0, j = 0; i < sameBook.length; i++) {
         if (sameBook[i] != 0) {
            SmallerLibrary[j++] = sameBook[i];
         }
      }
    

    【讨论】:

      【解决方案3】:

      感谢大家的帮助,在 Jeff Ward 和 Phillip 的帮助下,我想出了如何让我的程序按照我的意愿行事。新的工作代码如下! :)

          //Beginning of ModifyBooks Class
          Books[] Library = new Books[10];
      
          Library[0] = new Books();
          Library[1] = new Books("Lovely Bones", 12345, 20);
          Library[2] = new Books("Birds in a Fence", 123456, 13);
          Library[3] = new Books("Hunger Games", 1234567, 14);
          Library[4] = new Books("Titanic", 12345678, 12);
          Library[5] = new Books("Heroes", 123456789, 21);
          Library[6] = new Books(Library[0]);
          Library[7] = new Books(Library[0]);
          Library[8] = new Books(Library[2]);
          Library[9] = new Books(Library[3]);
      
          //Changing all prices of books
          for (int i = 0 ; i < Library.length ; i++){
              Library[i].price = i + 5;
          }
      
          //Keyboard configuration
          Scanner kb = new Scanner(System.in);
      
          //Getting user information
          System.out.println("Please enter a book's title:");
          String UserTitle = kb.nextLine();
      
          System.out.println("Please enter a book's ISBN Number:");
          int UserISBN = kb.nextInt();
      
          System.out.println("Your entered book's title is " + UserTitle + " and the ISBN is " + UserISBN);
      
          int[] sameBook = new int[10]; //new array that will carry the index of each match
          int counter = 0; //tracks number of matches
          int globalMin = 200; //lowest price for the matches books, initialized at 200 so the program functions properly
      
      
          //Finding the matches in main array, and casting their indexes in another array
          for (int i = 0 ; i < Library.length ; i++ ){
              if (UserTitle.equalsIgnoreCase(Library[i].title) && UserISBN == Library[i].ISBN){
                  sameBook[i] = i;
                  counter++;
                      if (Library[i].price < globalMin){
                          globalMin = Library[i].price;
                      }
              }
              else {
                  sameBook[i] = 0;
              }
      
          }
      
          //Creating a new array that only contains the amount of matches there are, containing their indexes
          int[] smallerLibrary = new int[counter];
          int j = 0;
      
          for (int i = 0 ; i < sameBook.length ; i++){
              if (sameBook[i] != 0){
                  smallerLibrary[j++] = sameBook[i];
              }
          }
      
          //Text notifying user of matches and telling the, what the new prices will be
          for (int i = 0 ; i < smallerLibrary.length ; i++){
              System.out.println("Index number [" + smallerLibrary[i] + "] matches both the ISBN and title. The price was $" + Library[smallerLibrary[i]].price + " and is being changed to the lowest price of $" + globalMin + ".");
          }
      
          //Changing the prices from the matches to the lowest prices
          for (int i = 0 ; i < Library.length ; i++){
              if (UserTitle.equalsIgnoreCase(Library[i].title) && UserISBN == Library[i].ISBN){
              Library[i].price = globalMin;
              System.out.println("Change was made for index number "+i+".");
              }
          }
      

      【讨论】:

      • 我回答你的问题了吗?
      猜你喜欢
      • 2015-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-29
      • 1970-01-01
      • 2015-07-16
      • 2020-10-31
      相关资源
      最近更新 更多