日期:2014-05-20 浏览次数:20769 次
public class Book { private String author; private String bookName; private int bookPrice; }
import java.util.List; import java.util.ArrayList; class Book { private String author; private String bookName; private int bookPrice; public Book(String author,String bookName,int bookPrice) { this.author=author; this.bookName=bookName; this.bookPrice=bookPrice; } public String getAuthor() { return this.author; } public String getBookName() { return this.bookName; } public int getBookPrice() { return this.bookPrice; } } public class BookTest { public static void main(String[] args) { List<Book> list=new ArrayList<Book>(); list.add(new Book("张三","书名1",100)); list.add(new Book("李四","书名2",110)); list.add(new Book("王五","书名3",120)); String[][] child=new String[list.size()][3]; for(int i=0;i<child.length;i++) { for(int j=0;j<3;j++) { child[j][0]=list.get(j).getAuthor(); child[j][1]=list.get(j).getBookName(); child[j][2]=String.valueOf(list.get(j).getBookPrice()); } } for(String[] test:child) for(String s:test) System.out.print(s+" "); } }
------解决方案--------------------
for (int i = 0; i < books.size(); i++) {
Book book = books.get(i);
child[i][0] = book.getAuthor();
child[i][1] = book.getBookName();
child[i][2] = book.getBookPrice() + "";
}
对的,主要就是这个for循环。