如何为arraylist项指定int值

时间:2015-07-19 05:24:16

标签: java arrays arraylist

ArrayList<Book> books=new ArrayList<Book>();
Book b1=new Book();
b1.setName("Physics");
b1.setCopy(10);
books.add(b1);
b1.setName("Physics");
b1.setCopy(10);
books.add(b1);
b1.setName("Differential Calculus");
b1.setCopy(10);
books.add(b1);
b1.setName("Integral Calculus");
b1.setCopy(10);
books.add(b1);
b1.setName("Introduction to English");
b1.setCopy(10);
books.add(b1);
b1.setName("History of the Philippines");
b1.setCopy(10);
books.add(b1);
b1.setName("Java for beginners");
b1.setCopy(10);
books.add(b1);
b1.setName("Cisco");
b1.setCopy(10);
books.add(b1);
b1.setName("Animal Kingdom");
b1.setCopy(10);
books.add(b1);
b1.setName("College Algebra");
b1.setCopy(10);
books.add(b1);
b1.setName("Trigonometry");
b1.setCopy(10);
books.add(b1);

我正在尝试这样做

for(Book book : books)
{
    System.out.println(book.getName() + " " + String.valueOf(book.getCopy()) + "copies");
}
String borrow=scan.next();
if(borrow.equals("physics"))
{
    int copies=10;
    System.out.println("The book you borrowed is Physics.");
    System.out.println("You borrowed copy "+stack.pop()+" of Physics");
    copies--;
    bbooks.add("Physics");
    System.out.println("Physics only has "+copies+" copies left.");
}

输出结果为:

  

三角学10copies
  三角学10copies
  三角学10copies
  三角学10copies
  三角学10copies
  三角学10copies
  三角学10copies
  三角学10copies
  三角学10copies
  三角学10copies

2 个答案:

答案 0 :(得分:1)

您可以使用HashMap而不是列表。

它会是这样的:

Map<String, Integer> map = new HashMap<String, Integer>();

map.put("Differential Calculus", 10);
map.put("Physics", 12);
map.put("Integral Calculus", 20);

或者继续使用列表并创建一个这样的类:

public class Subject {
    private String name;
    private int copies;

    public Subject(String name, int copies) {
        this.name = name;
        this.copies = copies;
    }

    public String getName() {
        return name;
    }

    public int getCopies() {
        return copies;
    }
}

这样你可以使用列表......

ArrayList<Subject> list = new ArrayList<Subject>();

list.add(new Subject("Differential Calculus", 10));
list.add(new Subject("Physics", 12));
list.add(new Subject("Integral Calculus", 20));

答案 1 :(得分:0)

制作名为Java的自定义Book课程。添加两个类变量String name;int copies。设置getter和setter。

public class Book {
    private String name;
    private int copy;


    public Book() {

    }

    //Add this constructor, and keep the empty constructor too

    public Book(String name, int copy) {
        this.name = name;
        this.copy = copy;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setCopy(int copy) {
        this.copy = copy;
    }

    public String getName() {
        return name;
    }

    public int getCopy() {
        return copy;
    }
}

然后从Main班级:

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

方法1:

Book b1 = new Book();
b1.setName("Physics");
b1.setCopy(10);
bookList.add(b1);

Book b2 = new Book();
//b2.setName....
//do this for all books

方法2:使用此方法。这是一种更有效的方式:

bookList.add(new Book("Trigonometry", 10));
bookList.add(new Book("Differential Calculus", 10));
//Do this for all books.

这部分保持不变:

int index = 1;
for(Book book : bookList) {
    System.out.println(index + " " + book.getName() + " " + String.valueOf(book.getCopy()) + "copies");
    index++;
}



int borrow = scan.nextInt();
if(borrow==0){
    System.out.println("The book you borrowed is Physics.");

    Book book = (Book) bookList.get(borrow);
    book.setCopy((book.getCopy() - 1));
}

我认为这就是你要找的东西。