当前位置: 首页 > 知识库问答 >
问题:

在同名数组中查找对象的索引,按值排序,然后更改其值

广绪
2023-03-14

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

books类如下所示:class books{private String title;private int isbn;private int price;

        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);

}

}

共有3个答案

云宏儒
2023-03-14

感谢大家的帮助,在杰夫·沃德和菲利普的帮助下,我想出了如何让我的程序做我想做的事情。新的工作代码在下面!:)

    //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+".");
        }
    }
燕琨
2023-03-14

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

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

相同的书长度总是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];
     }
  }
邹开畅
2023-03-14

请考虑以下策略:

  1. 遍历数组中的每个book对象,并检查它是否与用户输入匹配。
  2. 如果图书匹配,则将其索引存储在数组中以备以后使用,如果图书的价格低于先前设置的价格,则更新全局最小价格变量的值(控制第一次通过情况下的初始值)
  3. 完成对列表的迭代后,使用存储每个匹配图书索引的数组,并使用全局最小价格变量更新它们的价格字段
 类似资料:
  • 我有一个排序的值数组和一个值,如下所示: 我可以找到在保持排序顺序的同时将插入到中的值的索引: 代码很好而且紧凑,但我有一种直觉,在幕后这是非常低效的:因为不知道数组已排序,所以它必须检查所有值。 有没有更好的方法找到这个指数值? 注意:我对实际合并到不感兴趣,我只想要索引值。

  • 问题内容: 我有一个带有数字值的字符串键数组,可用于具有每个标签出现次数的标签列表中,因此: 这是为了我可以按降序显示标签列表,因此: 我可以使用 arsort 通过出色的值进行反向排序,但是我还希望具有相同数字值的所有标签都按字母顺序排序,因此最终结果可以是: 有办法可以做到吗?我猜想 usort 可能是要走的路,但是我看了php.net上的示例,我的眼睛呆呆了!非常感谢!!! 问题答案: 看一

  • 问题内容: 我有一个pandas数据框。我想按升序打印其列之一的唯一值。这就是我的做法: 问题是我得到了输出。 问题答案: 从iterable中返回一个新的排序列表。 码 输出值

  • 问题内容: 例如我有这个数组: 如何从对象获取价值指数?例如“ Country”:“ Austria”,该指数为3 问题答案: 您可以在最新的浏览器中使用,但Internet Explorer中不支持此功能,只有Edge 为了在IE9及更高版本中提供支持,您可以改用

  • 问题内容: 假设我有 有没有一种有效的numpy方法来查找值变化的每个索引?例如,我想要一些结果, 如果某些numpy例程无法做到这一点,那么在python中执行此操作的快速方法是什么?推荐给一些很好的numpy教程对我来说也很有用,因为我是个numpy初学者。 问题答案: 您可以通过将每个元素与其相邻元素进行比较来以numpy的形式获得此功能; 要获取索引,请使用“ where”功能 在这里,您