String[] arr = "张三,15,20".split(","); Student stu =new Student(arr[0],Integer.parseInt(arr[1],arr[2]));哪里错误
时间: 2024-01-25 07:03:48 浏览: 57
在创建 Student 对象时,Integer.parseInt() 方法只接受一个参数,但是在这里传递了两个参数。应该将代码修改为:
```
String[] arr = "张三,15,20".split(",");
Student stu = new Student(arr[0], Integer.parseInt(arr[1]), Integer.parseInt(arr[2]));
```
这样就可以正确地将字符串中的数字转换为整数,并创建一个新的 Student 对象。
相关问题
帮我解释以下代码import java.io.*; import java.time.LocalDate; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class Demo5 { public static void main(String[] args) throws IOException { ArrayList<Student> arr = new ArrayList<>(); File f1 = new File("input.txt"); FileInputStream fin = new FileInputStream(f1); BufferedReader br = new BufferedReader(new InputStreamReader(fin)); String msg = br.readLine(); while (msg != null) { if (msg.equals("")) { msg = br.readLine(); continue; } String[] stu = msg.split(" "); String num = stu[0]; String na = stu[1]; String da = stu[2]; String gen = stu[3]; Student student = new Student(Integer.parseInt(num), na,LocalDate.parse(da), gen); arr.add(student); Collections.sort(arr,Comparator.comparing(Student::getDate)); msg = br.readLine(); } br.close(); fin.close(); FileWriter writer = new FileWriter("output.txt"); for (Student obj:arr ) { writer.write(String.valueOf(obj)); writer.write("\r\n"); } writer.close(); } static class Student { private Integer number; private String name; private LocalDate date; private String gender; public LocalDate getDate() { return date; } public Student(Integer number, String name, LocalDate date, String gender) { this.number = number; this.name = name; this.date = date; this.gender = gender; } @Override public String toString() { return number + " " + name + " " + date + " " + gender; } } }
这段代码主要实现了以下功能:
1. 从名为"input.txt"的文件中读取学生信息,每一行为一名学生的信息,包括学号、姓名、出生日期和性别,用空格分隔。
2. 将读取的学生信息存储在一个ArrayList中,每个元素为一个Student对象。
3. 对ArrayList中的学生对象按照出生日期进行排序。
4. 将排好序的学生信息写入名为"output.txt"的文件中,每行为一名学生的信息,格式与输入文件相同。
其中,Student类表示一个学生对象,包括学号、姓名、出生日期和性别属性,并且重写了toString()方法以便于输出。通过LocalDate类来处理日期信息,Comparator.comparing()方法用于比较出生日期并进行排序。
将如下题按LinkedList集合或HashSet集合各实现一次。在HashSet中需重写hashCode()和equals()方法来判断元素是否重复。 按要求完成下列操作: 1、创建一个学生类(包含学号,姓名,性别,年龄属性,无参及有参构造函数) 2、创建两个集合 对象 arr1和arr2 3、向arr1集合中添加学生对象stu1("2001","zhangsan","男",19),stu2("2002","lisi","男",20),查看集合的长度 4、向arr2集合中添加学生对象stu3("2003","zhang","男",19),stu4("2004","lisi","女",20),查看集合的长度 5、将arr1的元素复制到arr2中,查看arr2集合的长度 6、删除arr2中索引1的元素并查看其值 7、移除arr1中所有的元素java
使用LinkedList集合实现:
```java
import java.util.LinkedList;
public class Student {
private String id;
private String name;
private String gender;
private int age;
public Student() {}
public Student(String id, String name, String gender, int age) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
}
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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
'}';
}
}
public class Main {
public static void main(String[] args) {
LinkedList<Student> arr1 = new LinkedList<>();
LinkedList<Student> arr2 = new LinkedList<>();
// 向arr1中添加学生对象
Student stu1 = new Student("2001", "zhangsan", "男", 19);
Student stu2 = new Student("2002", "lisi", "男", 20);
arr1.add(stu1);
arr1.add(stu2);
System.out.println("arr1的长度为:" + arr1.size());
// 向arr2中添加学生对象
Student stu3 = new Student("2003", "zhang", "男", 19);
Student stu4 = new Student("2004", "lisi", "女", 20);
arr2.add(stu3);
arr2.add(stu4);
System.out.println("arr2的长度为:" + arr2.size());
// 将arr1的元素复制到arr2中
arr2.addAll(arr1);
System.out.println("复制后arr2的长度为:" + arr2.size());
// 删除arr2中索引1的元素并查看其值
Student removed = arr2.remove(1);
System.out.println("被删除的元素为:" + removed);
// 移除arr1中所有的元素
arr1.clear();
System.out.println("arr1的长度为:" + arr1.size());
}
}
```
使用HashSet集合实现:
```java
import java.util.HashSet;
import java.util.Objects;
public class Student {
private String id;
private String name;
private String gender;
private int age;
public Student() {}
public Student(String id, String name, String gender, int age) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
}
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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
'}';
}
@Override
public int hashCode() {
return Objects.hash(id, name, gender, age);
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Student student = (Student) obj;
return age == student.age &&
Objects.equals(id, student.id) &&
Objects.equals(name, student.name) &&
Objects.equals(gender, student.gender);
}
}
public class Main {
public static void main(String[] args) {
HashSet<Student> arr1 = new HashSet<>();
HashSet<Student> arr2 = new HashSet<>();
// 向arr1中添加学生对象
Student stu1 = new Student("2001", "zhangsan", "男", 19);
Student stu2 = new Student("2002", "lisi", "男", 20);
arr1.add(stu1);
arr1.add(stu2);
System.out.println("arr1的长度为:" + arr1.size());
// 向arr2中添加学生对象
Student stu3 = new Student("2003", "zhang", "男", 19);
Student stu4 = new Student("2004", "lisi", "女", 20);
arr2.add(stu3);
arr2.add(stu4);
System.out.println("arr2的长度为:" + arr2.size());
// 将arr1的元素复制到arr2中
arr2.addAll(arr1);
System.out.println("复制后arr2的长度为:" + arr2.size());
// 删除arr2中索引1的元素并查看其值
int index = 1;
Student removed = null;
for (Student student : arr2) {
if (index == 1) {
removed = student;
break;
}
index++;
}
arr2.remove(removed);
System.out.println("被删除的元素为:" + removed);
// 移除arr1中所有的元素
arr1.clear();
System.out.println("arr1的长度为:" + arr1.size());
}
}
```
阅读全文