使用数组和数组列表的Java类
我正在努力学习Java,并且在理解类方面非常困难。我在一个类中有一个字符串数组,需要将其放入对象的arrayList中,然后在另一个类的方法中使用arrayList的getter和setter。以下是我的一些代码:使用数组和数组列表的Java类,java,arrays,class,inheritance,arraylist,Java,Arrays,Class,Inheritance,Arraylist,我正在努力学习Java,并且在理解类方面非常困难。我在一个类中有一个字符串数组,需要将其放入对象的arrayList中,然后在另一个类的方法中使用arrayList的getter和setter。以下是我的一些代码: public class Store { public static void main(String[] args) { Book book1 = new Book(); Book book2 = new Bo
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()));
}
}
公共类存储
{
公共静态void main(字符串[]args){
Book book1=新书();
Book book2=新书();
Book book3=新书();
printAll();
}
public void printAll(){
用于(弦书:手头的书){
super.print()
}
}
}
公共类图书商店
{
私有字符串标题;
私有字符串作者;
私人国际年;
int[]stock=new int[4];//5家商店中的每一家都有多少本书
String[]books={“战争与和平,列夫·托尔斯泰,1869,12,7,3,9”,
“小妇人,路易莎·梅·奥尔科特,1868年4月5日2月8日”,
“杀死一只知更鸟,哈珀·李,1960,21,18,13,6”,
};
ArrayList booksOnHand=新建ArrayList();
公共图书(字符串标题、字符串作者、整数年、整数[]股票)
{
this.title=标题;
this.author=作者;
今年=年;
这个。股票=股票;
}
公共字符串getTitle()
{
返回标题;
}
公共字符串getAuthor()
{
返回作者;
}
公共字符串getYear()
{
回归年;
}
public int[]getStock()
{
退货;
}
公共无效集合标题(字符串标题)
{
this.title=标题;
}
公共void setAuthor(字符串编写器)
{
this.author=作者;
}
公共年(国际年)
{
今年=年;
}
公共无效设置存储(int count1、int count2、int count3、int count4)
{
股票[0]=count1;
股票[1]=count2;
股票[2]=count3;
股票[3]=count4;
}
作废打印()
{
System.out.println(“标题:“+getTitle()+”\tAuthor:“+getAuthor()+”\tYear:“+getYear()+”\tStock:“+Arrays.toString(getStock())”;
}
}
我尝试了更多的代码,包括Collections.addAll(booksOnHand,books)代码>
但是我不知道把arrayList放在哪里,以及如何实例化它,以便在我的另一个类中使用它。提前感谢所有愿意帮忙的人 我做了一些更正,请尝试下面的代码。我对代码添加了一些注释
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()));
}
}
import java.util.ArrayList;
导入java.util.List;
公共类商店{
private static List booksOnHand=new ArrayList();
公共静态void main(字符串[]args){
//您应该使用您的构造创建您的对象
book1=新书(《战争与和平》,《列夫·托尔斯泰》,1869、12、7、3、9);
Book book2=新书(“小妇人”,“路易莎·梅·奥尔科特”,1868年,第4、5、2、8页);
book3=新书(《杀死一只知更鸟》,《哈珀·李》,1960年,21,18,13,6);
//将它们添加到列表中
新增(第1册);
增加(第2册);
增加(第3册);
printAll();
}
公共静态void printAll(){
//使用自己的对象方法打印它们。
用于(书籍:BookOnHand){
book.print();
}
}
}
导入java.util.array;
//这本书不应该扩展存储空间,所以我删除了它
公共课堂用书{
私有字符串标题;
私有字符串作者;
私人国际年;
int[]stock=new int[4];//5家商店中的每一家都有多少本书
//使用int…(vararg)可以添加商店库存
公共书籍(字符串标题、字符串作者、整数年、整数…股票){
this.title=标题;
this.author=作者;
今年=年;
这个。股票=股票;
}
公共字符串getTitle(){
返回标题;
}
公共字符串getAuthor(){
返回作者;
}
公共int getYear(){
回归年;
}
public int[]getStock(){
退货;
}
公共无效集合标题(字符串标题){
this.title=标题;
}
公共void setAuthor(字符串编写器){
this.author=作者;
}
公共年(国际年){
今年=年;
}
//使用varargs代替“int count1、int count2、int count3、int count4”
公共无效设置库存(内部…库存){
这个。股票=股票;
}
作废打印(){
System.out.println(“标题:“+getTitle()+”\tAuthor:“+getAuthor()
+\t目录:“+getYear()+”\t目录:
+toString(getStock());
}
}
印刷品:
标题:战争与和平作者:列夫·托尔斯泰年份:1869股票:[12,7,3,9]
标题:小妇人作者:路易莎·梅·奥尔科特年份:1868股票:[4,5,2,8]
标题:杀死一只知更鸟作者:哈珀·李年份:1960年股票:[21,18,13,6]
根据Java类在现实生活中所表示的对象来考虑它们。这本书不应该延伸到商店。当您使用extends时,您会说“这个对象类似于这个对象,但更具体。”
所以你可能有一个叫做Vehicle的类。所有车辆都有发动机。但是你可能有不同种类的交通工具,可以做不同的事情。所以你们可能会有一辆延伸车辆的普通车和一艘延伸车辆的普通船。它们都有发动机,但运动方式不同。这个概念叫做继承。在你的情况下,没有必要使用它
类和对象之间也存在差异。课堂就像一幅蓝图。一流的汽车知道汽车需要知道的所有事情。从Car类创建对象时,指定
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
}
}