Java將字符串按逗號拆分

代碼:
public class TestXX {
    public static void main(String[] args) {
        String s = "ss,f,a";
        String[] iv = s.split(",");
        List<Student> ss = new ArrayList<>();
        Student s1 = new Student();
        for (int i = 0;i<iv.length;i++){
            Student s2 = new Student();
            s2.setName(iv[i]);
            System.out.println(s2.getName());
            ss.add(s2);
        }
        System.out.println(ss.toString());
    }
}

結果:

ss
f
a
[Student{name='ss', age=0, math=0}, Student{name='f', age=0, math=0}, Student{name='a', age=0, math=0}]

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章