查找具有相同名称的数组中的对象索引,按值对它们进行排序,然后更改它们的值

时间:2014-04-12 01:24:42

标签: java arrays sorting loops object

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

书籍课程如下:     class Books {             私有字符串标题;             private int ISBN;             私人价格;

        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 个答案:

答案 0 :(得分:0)

考虑以下策略:

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

答案 1 :(得分:0)

问题是你正在迭代不同长度的数组导致ArrayIndexOutOfBounds。

如果您输入&#34; Lovely Bones&#34;,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];
     }
  }

答案 2 :(得分:0)

感谢大家的帮助,我想出了如何让我的程序在Jeff Ward和Phillip的帮助下做我想做的事。新的WORKING代码如下! :)

    //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+".");
        }
    }
相关问题