【问题标题】:Java classes using arrays and arrayLists使用数组和 arrayLists 的 Java 类
【发布时间】:2016-02-16 23:11:50
【问题描述】:

我正在努力学习 Java,但在理解类方面遇到了很大的困难。我在一个类中有一个 String 数组,我需要将它放入对象的 arrayList 中,然后在另一个类的方法中为 arrayList 使用 getter 和 setter。这是我的一些代码:

   public class Store
    {
       public static void main(String[] args) {
           Book book1 = new Book();
           Book book2 = new Book();
           Book book3 = new Book();

           printAll();
        }

       public void printAll(){
           for(String book : booksOnHand){
               super.print()
           }
       }      
    }

    public class Book extends Store
    {
        private String title;
        private String author;
        private int year;
        int[] stock = new int[4];//how many books are on hand at each of 5 stores

        String [] books = {"War and Peace, Leo Tolstoy, 1869, 12, 7, 3, 9", 
                           "Little Women, Louisa May Alcott, 1868, 4, 5, 2, 8",
                           "To Kill A Mockingbird, Harper Lee, 1960, 21, 18, 13, 6",
                          };

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

        public Book(String title, String author, int year, int [] stock)
        {
            this.title = title;
            this.author = author;
            this.year = year;
            this.stock = stock;       
        }

        public String getTitle()
        {
            return title;
        }

        public String getAuthor()
        {
            return author;
        }

        public String getYear()
        {
            return year;
        }

        public int[] getStock()
        {
            return stock;
        }

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

        public void setAuthor(String author)
       {
           this.author = author;
       }

        public void setYear(int year)
       {
           this.year = year;
       }

        public void setStock(int count1, int count2, int count3, int count4)
       {
           stock[0] = count1;
           stock[1] = count2;
           stock[2] = count3;
           stock[3] = count4;
       }

        void print() 
        {   
                System.out.println("Title: " + getTitle() + "\tAuthor: " + getAuthor() + "\tYear: " + getYear() + "\tStock: " + Arrays.toString(getStock()));
        }
    }

我尝试过更多代码,包括Collections.addAll(booksOnHand, books);

但我不知道将 arrayList 放在哪里以及如何实例化它,以便我可以在我的其他类中使用它。提前感谢所有愿意提供帮助的人!

【问题讨论】:

  • 您是否阅读过编译器的错误信息?他们告诉你哪里出了问题。你不应该忽视它们。另外,为什么书会扩展商店?一本书不是商店。所以 Book 不应该扩展 Store。为什么一本书包含其他书?为什么一本书有库存?尝试模拟现实:商店有书。一本书没有书。

标签: java arrays class inheritance arraylist


【解决方案1】:

从它们在现实生活中所代表的对象的角度来考虑 Java 类。书不应该扩展存储。当您使用扩展时,您是在说“这个对象与这个对象类似,但更具体。”

所以你可能有一个名为 Vehicle 的类。所有车辆都有发动机。但是你可能有不同种类的车辆来做不同的事情。因此,您可能有一个扩展 Vehicle 的 Car 类和一个扩展 Vehicle 的 Boat 类。他们都有引擎,但他们的移动方式不同。这个概念称为继承。在您的情况下,无需使用它。

类和对象也有区别。一个类就像一个蓝图。 Class Car 知道汽车需要知道的所有事情。当您从 Car 类创建对象时,您指定颜色、速度等属性。这就是您不需要标题数组等的原因。蓝图不需要具体的值,只需要知道会有一个名为 title 的 String 会在你构建这本书时包含标题。

在你的情况下,想想书店是如何运作的。书店的一本书的记录是否知道其他商店是否有该书。没有。

我还会有一个名为 main 的不同类,其中你有你的 main 方法,然后将 Store 视为一个代表存放书籍的商店的类。将您的 ArrayList 放在此类中,并使用存储中的方法来访问信息,例如每个标题的手头有多少本书。

一般设置应该更像:

public class Main {

    public static void main(String[] args) {

        Store barnesAndNoble = new Store();

        Book book1 = new Book("War and Peace", "Leo Tolstoy", 1869, 12, 7, 3, 9);
        Book book2 = new Book("Little Women", "Louisa May Alcott", 1868, 4, 5, 2, 8);
        Book book3 = new Book("To Kill A Mockingbird", "Harper Lee", 1960, 21, 18, 13, 6);

        barnesAndNoble.add(book1);
        barnesAndNoble.add(book2);
        barnesAndNoble.add(book3);

        barnesAndNoble.printAll();

        Store amazon = new Store();
        amazon.add(book1);
        amazon.add(book2);
        amazon.add(book3);

        amazon.printAll();
    }
}

public class Book {
    //code pertaining to books, no stock information
}

public class Store {

    private List<Book> booksOnHand;

    public Store() {
        booksOnHand = new ArrayList<Book>();
    }

    public void add(Book book) {
        booksOnHand.add(book);
    }

    //other methods for accessing the list of books, returning inventory numbers from list, or other code pertaining to listing store information

    public void printAll(){
        //print each books desired information
    }

}

【讨论】:

  • 非常感谢您的解释!原始数组如何适应这个?我可以在 Book book1 = new Book(super.books[0]) 中引用它吗?
【解决方案2】:

我做了一些更正,试试下面的代码。我在代码上添加了一些 cmets。

import java.util.ArrayList;
import java.util.List;

public class Store {
    private static List<Book> booksOnHand = new ArrayList<Book>();
    public static void main(String[] args) {
        // you should create you object with your constructure
        Book book1 = new Book("War and Peace", "Leo Tolstoy", 1869, 12, 7, 3, 9);
        Book book2 = new Book("Little Women", "Louisa May Alcott", 1868, 4, 5, 2, 8);
        Book book3 = new Book("To Kill A Mockingbird", "Harper Lee", 1960, 21, 18, 13, 6);
        // add them into a list
        booksOnHand.add(book1);
        booksOnHand.add(book2);
        booksOnHand.add(book3);
        printAll();
    }

    public static void printAll() {
        // print them with its own object method.
        for (Book book : booksOnHand) {
            book.print();
        }
    }
}


import java.util.Arrays;
// book shouldn't extends store, so I removed that
public class Book {
    private String title;
    private String author;
    private int year;
    int[] stock = new int[4];// how many books are on hand at each of 5 stores
    
    // with int...(vararg) you can add stocks of stores
    public Book(String title, String author, int year, int...stock) {
        this.title = title;
        this.author = author;
        this.year = year;
        this.stock = stock;
    }

    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }

    public int getYear() {
        return year;
    }

    public int[] getStock() {
        return stock;
    }

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

    public void setAuthor(String author) {
        this.author = author;
    }

    public void setYear(int year) {
        this.year = year;
    }

    // use varargs instead of "int count1, int count2, int count3, int count4"
    public void setStock(int... stock) {
        this.stock = stock;
    }

    void print() {
        System.out.println("Title: " + getTitle() + "\tAuthor: " + getAuthor()
                + "\tYear: " + getYear() + "\tStock: "
                + Arrays.toString(getStock()));
    }
}

打印:

书名:战争与和平作者:列夫·托尔斯泰年份:1869 股票:[12,7,3,9]

书名:小妇人作者:Louisa May Alcott 年份:1868 股票:[4, 5, 2, 8]

标题:杀死一只知更鸟作者:Harper Lee 年份:1960 股票:[21,18,13,6]

【讨论】:

  • 非常感谢您的帮助!虽然它确实有帮助,但我仍然必须使用原始数组并找到一种将其放入 arrayList 的方法,所以会继续寻找。谢谢!
猜你喜欢
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多