使用数组和数组列表的Java类

时间:2015-11-15 21:44:30

标签: java arrays class inheritance arraylist

我正在努力学习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放在何处以及如何实例化它,以便我可以在其他类中使用它。提前感谢所有愿意提供帮助的人!

2 个答案:

答案 0 :(得分:1)

我做了一些更正,请尝试下面的代码。我在代码中添加了一些评论。

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]

答案 1 :(得分:1)

根据它们在现实生活中所代表的对象来考虑Java类。书不应该扩展商店。当你使用扩展时,你会说&#34;这个对象就像这个,但更具体。&#34;

所以你可能有一个名为Vehicle的类。所有车辆都有发动机。但是你可能会有不同类型的车辆做不同的事情。因此,您可能会有一个可以扩展车辆的类Car和一个可以扩展车辆的类船。它们都有发动机,但它们的动作不同。这个概念称为继承。在你的情况下,没有必要使用它。

类和对象之间也存在差异。一个类就像一个蓝图。 Class Car了解汽车需要知道的所有事情。从Car类创建对象时,可以指定颜色,速度等属性。这就是您不需要标题数组等的原因。蓝图不需要特定的值,只知道在构建书时会有一个名为title的String,它将包含标题。

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

我还有一个名为main的不同类,您可以使用main方法,然后将Store视为表示存放书籍的商店的类。将您的ArrayList放在这个类中,并使用store中的方法来访问每个标题手头有多少本书的信息。

一般设置应该更像:

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
    }

}