简体   繁体   English

我在这个ArrayList中做错了什么?

[英]What am I doing wrong in this ArrayList?

I am suppose to create a Book class with the instance fields of title and number of pages. 我想用标题和页数的实例字段创建一个Book类。

I am then suppose to complete 14 tasks. 然后,我想完成14个任务。 I won't list them all but it includes adding books, then replacing a book then adding more. 我不会列出所有内容,但包括添加书籍,然后替换书籍,然后添加更多书籍。

 The Tasks: 
 1. Add (book 1 - 350 pages)
 2.Add (book 2 - 72 pages)
 3.Replace Book 2 with (book 3 - 220 pages)
 4.Add (book 4 - 120pages)
 5.Add (book 5 - 90 pages at index position 1)
 6.Add (Book 6 - 120 pages)
7. Add (book 7 - 170 pages)
8. Add(Book 8 - 400 pages)
9. Output the title of the book at index position 3
10. Output the size of the ArrayList. 
11. Remove all books that contain 120 pages.
12. Find total num of pages in remaining books in the ArrayList.
12. Output the total number of pages in the remaining books in the ArrayList.
14. Using and enhanced for loop, ouput the current books in the ArrayLIst.

EDIT: THANK YOU ALL SO MUCH! 编辑:非常感谢! I have fixed, refined and almost finished my code. 我已经修复,完善并几乎完成了我的代码。 Only problem is the last part which I am suppose to use enhanced for loop to output the current books. 唯一的问题是我想使用增强的for循环输出当前书籍的最后一部分。 I get weird numbers whenever I do. 每当我这样做时,我都会得到奇怪的数字。 package Book; 包装书;

import java.util.ArrayList;

public class Book {
    private String title;
    private int pages;

    public static void main(String[] args) {
        ArrayList<Book> booklib = new ArrayList<Book>();
        booklib.add(new Book("Book 1", 350));
        booklib.add(new Book("Book 2", 72));
        booklib.set(1, new Book("Book3", 220));
        booklib.add(new Book("Book 4", 120));
        booklib.add(1, new Book("Book 5", 90));
        booklib.add(new Book("Book 6", 120));
        booklib.add(new Book("Book 7", 170));
        booklib.add(new Book("Book 8", 400));

        System.out.println(booklib.get(3).gettitle());
        System.out.println(booklib.size());

        for (int i = 0; i < booklib.size(); i++)
            if (booklib.get(i).getpages() == 120)
                booklib.remove(i);

        int sum = 0;
        for (int i = 0; i < booklib.size(); i++) {
            sum = sum + booklib.get(i).getpages();
        }

        System.out.println(sum);
        for (Book books : booklib)
            System.out.println(books);
    }

    public Book(String n, int p) {
        title = n;
        pages = p;
    }

    public int getpages() {

        return pages;
    }

    public String gettitle() {

        return title;
    }

}

From your code, there are some mistakes: 从您的代码中,有一些错误:

1 You declared an ArrayList named booklib, where Book objects are stored. 1您声明了一个名为booklib的ArrayList,其中存储了Book对象。

ArrayList<Book> booklib = new ArrayList<Book>();

You CANNOT add the books like this: 无法添加像这样的书:

  booklib.add("Book 1", 350);
  booklib.add("Book 2", 72);

You SHOULD add the books using the following way 应该使用以下方式添加图书

  booklib.add(new Book("Book 1", 350));
  booklib.add(new Book("Book 2", 72));

2 If you would like to print a book's title 2如果您想打印一本书的标题

You CAN NOT use: 不能使用:

  System.out.println(booklib.gettitle(3));

You SHOULD use the following way instaed. 应该使用以下方式安装。

  System.out.println(booklib.get(0).gettitle());//Populate the first book's title

3 Regarding the for-loop and its if-condition inside. 3关于for循环及其内部的if条件。

Change 更改

 for(int i=0; i<booklib.size; i++)
    if(booklib[i] == 120)

to

 for(int i=0; i<booklib.size(); i++)
    if(booklib.get(i).getpages() == 120)

If remove elements from List, you'd better use iterator instaed. 如果从List中删除元素,则最好使用instaed迭代器。

You should .add(new Book("Book 1", 350)); 您应该.add(new Book("Book 1", 350)); since the ArrayList has a Book type param. 因为ArrayList具有Book类型参数。

ArrayList<Book> booklib = new ArrayList<Book>();
booklib.add(new Book("Book 1", 350));
booklib.add(new Book("Book 2", 72));

To get the size of an ArrayList use size() not size 要获取ArrayList的大小,请使用size()而不是size

for(int i=0; i<booklib.size; i++)

// instead use the code below

for(int i = 0; i < booklib.size(); i++)

Also in the same for loop, if you want the loop to break after a number is found just do this (you don't need the else , and also you need getPages() ): 同样在for循环中,如果您希望在找到一个数字后中断循环,只需执行此操作(您不需要else ,也需要getPages() ):

for(int i=0; i<booklib.size; i++)
    if(booklib[i].getPages() == 120) {
        booklib.remove(i);
        break;
    }
}

Also you should enclose the main method as follows 另外,您应该将main方法封装如下

public static void main(String[] args)
{
    ArrayList<Book> booklib = new ArrayList<Book>();
    booklib.add(new Book("Book 1", 350));
    booklib.add(new Book("Book 2", 72));

    System.out.println(booklib.gettitle(3));
    System.out.println(booklib.size());

    for(int i=0; i<booklib.size; i++) {    // You should have brakets for loops that
        if(booklib[i] == 120)              // have multiple statements. This version
            booklib.remove(i);             // only has one, but your original code 
                                           // had multiple statements
    }
}

You current have a Book construct and get methods inside the main method. 您当前拥有一个Book构造,并在main方法内部获取方法。 That's a no no 那不是不

And last thing I noticed, gettitles() doesn't take an argument. 我注意到的最后一件事, gettitles()不带参数。 You currently have this 您目前有这个

System.out.println(booklib.gettitle(3));

// Should be something like this

System.out.println(booklib.get(0).gettitle());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM