我现在有一个集合List<Book>;里边有两个Book,public class Book {
private String author;
private String bookName;
private int bookPrice;
}我现在想。从List<Book> 中取出每一个属性放到一个二维数组中。
    public String[][]  child= {  
            { "author1", "bookName1", "bookPrice1"},  
            { "author2", "bookName2", "bookPrice2"},  
           { "author3", "bookName3", "bookPrice3"}
    }; 
需要怎么处理。

解决方案 »

  1.   

    List<Book> books = new ArrayList<Book>();Book book1 = new Book();
    book1.setAuthor("aa");
    book1.setBookName("aa");
    book1.setBookPrice(11);
    books.add(book1);Book book2 = new Book();
    book2.setAuthor("bb");
    book2.setBookName("bb");
    book2.setBookPrice(22);
    books.add(book2);String[][] child= new String[books.size()][3];
    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 (int i = 0; i < child.length; i++) {
    for (int j = 0; j < child[0].length; j++) {
    System.out.print(child[i][j] + "===");
    }
    System.out.println();
    }结果
    aa===aa===11===
    bb===bb===22===
      

  2.   

    public String[][] abcd(List<Book> b){
    int length=b.size();
    String[][] a=new String[length][3];
    for(int i=0;i<length;i++){
    Book temp=b.get(i);
    a[i][0] = temp.getAuthor();
    a[i][1] = temp.getBookName();
    a[i][2] = ""+temp.getBookPrice();
    }
    return a;
    }
      

  3.   


    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+" ");
    }
    }转的太别扭了,楼主凑活看!
      

  4.   

    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循环。