好友
阅读权限20
听众
最后登录1970-1-1
|
且,听风吟
发表于 2020-8-18 23:17
一直空指针异常,不知道哪里的问题从数组遍历所有内容,解析为学生对象,将学生在存于一个新的List集合
package day_8_18;
import class_test.Stu;
import java.util.ArrayList;
import java.util.List;
public class StudentTest_12 {
public static void main(String[] args) {
List<String> list=new ArrayList<>();
list.add("张三/18/男/99.5");
list.add("赵四/20/男/91");
list.add("王五/19/男/83");
list.add("陈六/18/男/59");
List<Student> list1=new ArrayList<>();
Student[] students=new Student[list.size()];
for (int i=0;i<list.size();i++){
students.setName((list.get(i).split("/")[0]));
//将年龄值转为int类型
students.setAge((Integer.parseInt(list.get(i).split("/")[1])));
students.setSex((list.get(i).split("/")[2]));
//将分数值转为int类型
students.setScore((Double.parseDouble(list.get(i).split("/")[3])));
}
for(int i=0;i<students.length;i++) System.out.println(students);
}
}
class Student{
private String name;
private int age;
private String sex;
private double score;
public Student() {
}
public Student(String name, int age, String sex, double score) {
this.name = name;
this.age = age;
this.sex = sex;
this.score = score;
}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
", score=" + score +
'}';
}
} |
-
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|