如何将数组列表转换为对象?

时间:2019-04-05 18:31:35

标签: java

我必须按字母顺序对包含名称/姓氏/日期/价格/按姓氏支付的对象进行排序,但是当我使用Collection.sort对对象进行排序并尝试重写该对象时,它表示-java.util。 ArrayList无法转换为dip107.Student。 Student是对象的名称(类Student实现了Serializable {)。

public Student(String name, String surname, String date, double price, double paid) {
    this.name = name;
    this.surname = surname;
    this.date = date;
    this.price = price;
    this.paid = paid;
}

public static void sortsurname() {
    List<String> sort  = new ArrayList<String>();

    try {
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename));
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("temp"));
        Student s;
        boolean EOF = false;
        while (!EOF) {
            try {
                s = (Student) 
                in.readObject();
                sort.add(s.surname.toString());
                Collections.sort(sort);
                out.writeObject(sort);
            } catch (EOFException e) {
                EOF = true;
            }
        }
        in.close();
        out.close();
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
    new File(filename).delete();
    new File("temp").renameTo(new File(filename));
}

1 个答案:

答案 0 :(得分:0)

这是工作代码。关键是您在读取文件中所写的内容。您可以通过在Student中实现 Comparable 接口或在 Collections.sort()方法中传递 Comparator 来编写排序。

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SomeClass {


    public static void main(String args[])throws Exception{
        Student s1 = new Student("zksjdf","sdfkj");
        Student s2 = new Student("yui","aqws");
        List<Student> s = new ArrayList<>();
        s.add(s1);
        s.add(s2);
        ObjectOutputStream out1 = new ObjectOutputStream(new FileOutputStream("out1.txt"));
        ObjectOutputStream out2 = new ObjectOutputStream(new FileOutputStream("out2.txt"));
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("out1.txt"));

        out1.writeObject(s); // line 27

        boolean EOF = false;
        while (!EOF) {
            try {
                List<Student> ss = (List<Student>) in.readObject(); // this cast depends on what you'r writing in the file on line 27
                Collections.sort(ss, new Comparator<Student>() {

                    @Override
                    public int compare(Student o1, Student o2) {
                        return o1.lastName.compareTo(o2.lastName);
                    }
                });
                System.out.println(ss);
                out2.writeObject(ss);

            } catch (EOFException e) {
                EOF = true;
            }
        }
        in.close();
        out1.close();
        out2.close();
    }

}


class Student implements Serializable{
    private static final long serialVersionUID = -6960348259392457529L;
    String lastName;
    String firstName;
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public Student(String lastName, String firstName) {
        super();
        this.lastName = lastName;
        this.firstName = firstName;
    }
    @Override
    public String toString() {
        return "{"+lastName + ", " + firstName+"}";
    }
}