簡體   English   中英

ArrayList重用單個對象,而不是創建新的對象

[英]ArrayList re-using single Object, instead of creating new ones

我正在研究一個簡單的程序,該程序讀取包含書名和相關參考號的文本文件。 每組信息都應轉換為Book對象,以便以后進行排序。 但是,在當前代碼中,每當應創建新Book時,它都會重新使用原始Book。 我在Book類中添加了一個計數器來跟蹤Book對象的數量。

該程序應使用書名和關聯的參考號創建一個新的Book對象。 為了解決此問題,我將進行哪些更改/添加?

主班

    private static ArrayList<Book> books = new ArrayList();

    public static void main(String[] args) {
        String path = "src//booklist.txt";
        boolean endOfFile = false;

        // try/catch for reading the file
        try {
            FileReader fr = new FileReader(path);
            BufferedReader br = new BufferedReader(fr);

            while (!endOfFile) {
                String line = br.readLine();

                if (line == null) {
                    endOfFile = true;
                } else {
                    books.add(new Book(null, 0));
                    books.get(books.size() - 1).setRefNum(Integer.parseInt(line));

                    String bookTitle = br.readLine();
                    books.get(books.size() - 1).setTitle(bookTitle);
                }
                System.out.println(books.get(books.size() - 1).toString());
            }
            // Closing reader and displaying results
            br.close();
        } catch (IOException e) {
            System.out.println(e.toString());
        }
    }
}

書類

private String bookTitle;
private int refNum;
private int numBooks;

public Book(String title, int referenceNumber) {
    this.bookTitle = title;
    this.refNum = referenceNumber;
    this.numBooks++;
}

public String getTitle() {
    return this.bookTitle;
}

public void setTitle(String title) {
    this.bookTitle = title;
}

public int getRefNum() {
    return this.refNum;
}

public void setRefNum(int referenceNumber) {
    this.refNum = referenceNumber;
}

public int getNumBooks() {
    return this.numBooks;
}

public String toString() {
    String message = "Book Title: " + this.bookTitle
            + "\nReference #: " + this.refNum
            + "\nBook #: " + this.numBooks
            + "\n";
    return message;
}

文字文件booktitles.txt

1
The Adventures of Tom Sawyer
2
Huckleberry Finn
4
The Sword in the Stone
6
Stuart Little
10
Treasure Island
12
The Secret Garden
14
Alice's Adventures in Wonderland
20
Twenty Thousand Leagues Under the Sea
24
Peter Pan
26
Charlotte's Web
31
A Little Princess
32
Little Women
33
Black Beauty
35
The Merry Adventures of Robin Hood
40
Robinson Crusoe
46
Anne of Green Gables
50
Little House in the Big Woods
52
Swiss Family Robinson
54
The Lion, the Witch and the Wardrobe
56
Heidi
66
A Winkle in Time
100
Mary Poppins

電流輸出

Book Title: The Adventures of Tom Sawyer
Reference #: 1
Book #: 1

Book Title: Huckleberry Finn
Reference #: 2
Book #: 1

Book Title: The Sword in the Stone
Reference #: 4
Book #: 1

Book Title: Stuart Little
Reference #: 6
Book #: 1

Book Title: Treasure Island
Reference #: 10
Book #: 1

Book Title: The Secret Garden
Reference #: 12
Book #: 1

Book Title: Alice's Adventures in Wonderland
Reference #: 14
Book #: 1

Book Title: Twenty Thousand Leagues Under the Sea
Reference #: 20
Book #: 1

Book Title: Peter Pan
Reference #: 24
Book #: 1

Book Title: Charlotte's Web
Reference #: 26
Book #: 1

Book Title: A Little Princess
Reference #: 31
Book #: 1

Book Title: Little Women
Reference #: 32
Book #: 1

Book Title: Black Beauty
Reference #: 33
Book #: 1

Book Title: The Merry Adventures of Robin Hood
Reference #: 35
Book #: 1

Book Title: Robinson Crusoe
Reference #: 40
Book #: 1

Book Title: Anne of Green Gables
Reference #: 46
Book #: 1

Book Title: Little House in the Big Woods
Reference #: 50
Book #: 1

Book Title: Swiss Family Robinson
Reference #: 52
Book #: 1

Book Title: The Lion, the Witch and the Wardrobe
Reference #: 54
Book #: 1

Book Title: Heidi
Reference #: 56
Book #: 1

Book Title: A Winkle in Time
Reference #: 66
Book #: 1

Book Title: Mary Poppins
Reference #: 100
Book #: 1

期望的輸出

Book Title: The Adventures of Tom Sawyer
Reference #: 1
Book #: 1

Book Title: Huckleberry Finn
Reference #: 2
Book #: 2

Book Title: The Sword in the Stone
Reference #: 4
Book #: 3

Book Title: Stuart Little
Reference #: 6
Book #: 4

Book Title: Treasure Island
Reference #: 10
Book #: 5

Book Title: The Secret Garden
Reference #: 12
Book #: 6

Book Title: Alice's Adventures in Wonderland
Reference #: 14
Book #: 7

Book Title: Twenty Thousand Leagues Under the Sea
Reference #: 20
Book #: 8

Book Title: Peter Pan
Reference #: 24
Book #: 9

Book Title: Charlotte's Web
Reference #: 26
Book #: 10

Book Title: A Little Princess
Reference #: 31
Book #: 11

Book Title: Little Women
Reference #: 32
Book #: 12

Book Title: Black Beauty
Reference #: 33
Book #: 13

Book Title: The Merry Adventures of Robin Hood
Reference #: 35
Book #: 14

Book Title: Robinson Crusoe
Reference #: 40
Book #: 15

Book Title: Anne of Green Gables
Reference #: 46
Book #: 16

Book Title: Little House in the Big Woods
Reference #: 50
Book #: 17

Book Title: Swiss Family Robinson
Reference #: 52
Book #: 18

Book Title: The Lion, the Witch and the Wardrobe
Reference #: 54
Book #: 19

Book Title: Heidi
Reference #: 56
Book #: 20

Book Title: A Winkle in Time
Reference #: 66
Book #: 21

Book Title: Mary Poppins
Reference #: 100
Book #: 22

我認為您誤解了代碼

books.add(new Book(null, 0));
books.get(books.size() - 1).setRefNum(Integer.parseInt(line));
String bookTitle = br.readLine();
books.get(books.size() - 1).setTitle(bookTitle);

每次都在創建一Book 但是它效率低下。 考慮

Book b = new Book(null,0);
books.add(b);
b.setRefNum(Integer.parseInt(line));
String bookTitle = br.readLine();
b.setTitle(bookTitle);

變量b在循環本地。 更好的是,使用構造函數

String bookTitle = br.readLine();
Book b = new Book(bookTitle,Integer.parseInt(line));
books.add(b);

關於澄清的問題:

numBooks是一個實例變量,因此每本書都有一個副本。 您需要使用books.size()來檢索列表中的條目數。 Book類中刪除numBooks ,這是沒有必要的。

在構造函數中使用適當的值,這樣就無需添加dummy書,隨后got可以獲取dummy書以進行更新

String bookTitle = br.readLine();
books.add(new Book(bookTitle, Integer.parseInt(line)));

更換線

books.add(new Book(null, 0));
books.get(books.size() -1).setRefNum(Integer.parseInt(line));

String bookTitle = br.readLine();
books.get(books.size() - 1).setTitle(bookTitle);

通過

String bookTitle = br.readLine();
books.add(new Book(bookTitle,Integer.parseInt(line)));

您的問題出在numBooks的Book類中。您不需要此類。 您可以將arrayList書籍及其索引用於輸出。

添加到Book類的計數器不是靜態的。 因此,它對於Book對象的每個新實例都是唯一的。 因此它將永遠是一個。

private int numBooks;

public Book(String title, int referenceNumber) {
    this.bookTitle = title;
    this.refNum = referenceNumber;
    this.numBooks++;
}

您沒有看到從ArrayList圖書對象添加的多本書嗎?

System.out.println(books.get(books.size() - 1).toString());

根據您當前的輸出,標題正在更改,但是書號沒有更改。

使numBooks靜態。 Static變量與類鏈接,而不是作為類的實例,因此,相同變量將在屬於同一類實例的所有對象之間共享。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM