第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

對象的數(shù)組列表

對象的數(shù)組列表

慕碼人8056858 2022-07-20 20:38:26
我正在使用 Java 開發(fā)圖書館系統(tǒng)。我想創(chuàng)建一個類Book和一個Borrow具有ArrayListBook 對象和一些方法的類,例如searchBook().在創(chuàng)建ArrayList時,我遇到了很多錯誤。部分Book課程:package  Book;public class Book {public static void main(String[] args) {    // TODO Auto-generated method stub}    /********************************/public String author;public String title;public int numcopies;public Book() {};public Book(String author, String title, int           numcopies)         {            this.author = author;            this.title = title;            this.numcopies = numcopies;    }/*********************************/Borrow班級:package Book;import java.util.ArrayList;public class Borrow  {    public Book book ;     ArrayList<Book> BookList = new ArrayList<Book>();    BookList.add(new Book("Author", "Title", 26));}
查看完整描述

2 回答

?
臨摹微笑

TA貢獻1982條經(jīng)驗 獲得超2個贊

您不能在類級別進行任何計算,這意味著您需要執(zhí)行以下操作


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

BookList.add(new Student("Author", "Title", 26));

在方法/函數(shù)內(nèi)部,例如


public class Borrow  {

    public static void main(String[] args) {

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

        BookList.add(new Book("Author", "Title", 26));

    }

}



查看完整回答
反對 回復 2022-07-20
?
慕桂英546537

TA貢獻1848條經(jīng)驗 獲得超10個贊

您的問題不在于 ArrayList,而在于您的語法。


如果要在構(gòu)造 Object 時創(chuàng)建包含 Student 的 ArrayList,則應聲明構(gòu)造函數(shù):


public Borrow() {

  bookList = new ArrayList<Book>();

  bookList.add(new Book("Author", "Title", 26));

}

然后為您的班級創(chuàng)建bookList一個字段:Borrow


public class Borrow  {


    private ArrayList<Book> bookList;


    public Borrow() {

      bookList = new ArrayList<Book>();

      bookList.add(new Book("Author", "Title", 26));

    }

}

你不能在你的班級中間有這樣的代碼。此外,您不應該有public字段,因為它會破壞封裝。


查看完整回答
反對 回復 2022-07-20
  • 2 回答
  • 0 關(guān)注
  • 93 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號