当前位置: 代码迷 >> J2SE >> 把String数组放入对象数组里,该如何处理
  详细解决方案

把String数组放入对象数组里,该如何处理

热度:79   发布时间:2016-04-24 00:30:54.0
把String数组放入对象数组里
我是从txt文本读取的信息,用split分离,现在取出来的数据是string----------337;赵娜;10;1;13488765008
string----------555;高鹏;20;0;13522914394
string----------71;李育强;30;0;13910654647
string----------125;单川;20;1;13701190684
string----------395;张五顺;32;0;13910031262
string----------412;张清会;30;0;13810517386
string----------429;郑卉;34;1;13911110805
string----------467;王蕾;23;0;13911365509
string----------497;鲁玉梅;14;1;13401083616
string----------508;邹温高;54;0;13910068170
string----------528;赵丽霞;23;1;13811729697
string----------531;李颂;43;1;13910012500
string----------167;郑涛;23;0;13811480772
string----------187;朱小弟;26;0;13691204441
string----------230;张学道;54;0;13910975297
string----------1253;赵曙芳;32;1;13601300886
string----------1262;赵蕊;45;0;13718019692
string----------1326;袁胜勇;65;0;13901091435
string----------830;张京;34;1;13683509602

我想把这些数据放入,Student对象数组中,不知道怎么放,新手求教。student包括id,name,age,sex,phone

------解决方案--------------------
Java code
package a;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.Random;class Student{    private int id  ;    private String name ;    private int age ;    private int sex;    private String phone ;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public int getSex() {        return sex;    }    public void setSex(int sex) {        this.sex = sex;    }    public String getPhone() {        return phone;    }    public void setPhone(String phone) {        this.phone = phone;    }    public Student() {            }    public Student(int id, String name, int age, int sex, String phone) {        super();        this.id = id;        this.name = name;        this.age = age;        this.sex = sex;        this.phone = phone;    }        public Student(String[] arr){        if (arr.length  == 5){            this.id = Integer.valueOf(arr[0]).intValue() ;            this.name = arr[1] ;            this.age = Integer.valueOf(arr[2]).intValue() ;            this.sex = Integer.valueOf(arr[3]).intValue() ;            this.phone = arr[4] ;        }            }    @Override    public String toString() {        return "Student [id=" + id + ", name=" + name + ", age=" + age                + ", sex=" + sex + ", phone=" + phone + "]";    }        }public class ReadStudent {    /**     * @param args     * @author sunstar     * @throws FileNotFoundException     * @date 2012-7-4 上午11:14:15     */    public static void main(String[] args) {        // TODO Auto-generated method stub        List list = readStudent("D:\\std.txt") ;                Iterator it = list.iterator() ;        Student std =  null ;        System.out.println("========输出结果========") ;        while(it.hasNext()){            std = (Student) it.next() ;            System.out.println(std) ;        }            }    private static List readStudent(String fileName) {        BufferedReader br = null ;        String str = "" ;        List list = new ArrayList() ;        String tmp  ;        Student std = null ;        try {            br = new BufferedReader(new FileReader(fileName));            while ((str = br.readLine()) != null) {                tmp = str.replaceAll("string----------", "") ;                System.out.println(str + "   ===  " + tmp) ;                std = new Student(tmp.split(";" )) ;                System.out.println(std) ;                list.add(std) ;            }        } catch (Exception e) {            e.printStackTrace();        } finally {            try {                br.close();            } catch (IOException e) {                e.printStackTrace();            }        }        return list ;    }    }
  相关解决方案