日期:2014-05-20 浏览次数:20708 次
import java.util.List; import java.util.ArrayList; public class ShowPersons { public static void main(String[] args) { List<Person> persons = ListTest.getPersons(); int n=persons.size(); String[][] myArray=new String[n][]; //定义二维数组 for (int i=0;i<n;i++) //构造二维数组 { Person tempArray= persons.get(i); myArray[i]=tempArray.toStringAry(); //注意此处的写法 } //遍历数组 for(int i=0;i<myArray.length;i++){ for(int j=0;j<myArray[0].length;j++){ System.out.println("myArray["+i+"]["+j+"]="+myArray[i][j]); } } } } class ListTest{ public static List<Person> getPersons(){ List<Person> persons=new ArrayList<Person>(); Person p1=new Person("001","zhangsan","beijing","110"); Person p2=new Person("002","lsi","shanghai","120"); Person p3=new Person("003","wangwu","guangzhou","119"); persons.add(p1); persons.add(p2); persons.add(p3); return persons; } } class Person{ private String ID; private String name; private String address; public String[] toStringAry(){ String[] ary=new String[3]; if(ID!=null) ary[0]=ID; if(name!=null) ary[1]=name; if(address!=null) ary[2]=address; return ary; } public String getID() { return ID; } public void setID(String ID) { this.ID = ID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } private String telephone; public Person(){} public Person(String ID, String name, String address, String telephone) { this.ID = ID; this.name = name; this.address = address; this.telephone = telephone; } }