【问题标题】:How to copy by reference and deserialize a list of objects in java?java - 如何通过引用复制和反序列化java中的对象列表?
【发布时间】:2015-04-15 16:01:14
【问题描述】:

所以我是 Java 的初学者,我的教授让我们做这个作业,我一直在环顾四周,但似乎找不到适合我的代码类型的正确答案。我们必须创建一个类文件,然后创建一个公共类文件来测试它。

import java.io.Serializable;

public class Person implements Serializable {

    String Fname = new String();

    String MI = new String();

    String Lname = new String();

    String  age = new String();
    String gpa = new String();
//      int age = 20;

//      double gpa = 0.0;

    String Major = new String();

    String answer;


 //***********************************************
// The methods declared will go below 
// The first method is for the first name

public String getFname() {
   return Fname;
}

public void setFname(String Fname) {
       this.Fname = Fname;
    }


// This method is for the Middle Initial

public String getMI() {
   return MI;
}

 public void setMI(String MI) {
       this.MI = MI;
    }

// This method is for the Last name

public String getLname() {
       return Lname;    
}

    public void setLname(String Lname) {
       this.Lname = Lname;
    }

 // This method is for the Age

public String getAge() {
   return age;
}

public void setAge(String age) {
       this.age = age;
    }

// This method is for the GPA  

public String getGpa() {
   return gpa;
}

 public void setGpa(String gpa) {
       this.gpa = gpa;
    }

// This method is for the Major

public String getMajor() {
    return Major;
}
 public void setMajor(String Major) {
            this.Major = Major;
    }
}

Person.java 代码是 Serializable 应该被写入 fhm 文件,然后反序列化以便将文件的内容打印到终端 (Unix)。下面是读取类 Person 代码的 TestPerson 代码的主要代码。

import java.util.Scanner;
import java.io.*;

public class TestPerson {

public static void main(String[] args) throws IOException, ClassNotFoundException { 


Person[] Peeps = new Person[3];
Peeps[0] = new Person();
Peeps[1] = new Person();
Peeps[2] = new Person();

Scanner scan = new Scanner(System.in);
String Answer = new String();
String age1 = new String();
String gpa1 = new String();

for (int i = 0; i < Peeps.length - 1; i++) {
//  for (int i = 0; i < 3; i++) {   

System.out.print("What is the First Name? ");
Answer = scan.nextLine();
Peeps[i].setFname(Answer);

System.out.print("What is MI? ");
Answer = scan.nextLine();
Peeps[i].setMI(Answer);

System.out.print(" What is Last Name? ");
Answer = scan.nextLine();
Peeps[i].setLname(Answer);

System.out.print(" What is the Age? ");
age1 = scan.nextLine();
int age = Integer.parseInt(age1);
//  age = scan.nextInt();
Peeps[i].setAge(age1);

System.out.print(" What is the GPA? ");
gpa1 = scan.nextLine();
double gpa = Double.parseDouble(gpa1);
Peeps[i].setGpa(gpa1);

System.out.print(" What is the Major? ");
Answer = scan.nextLine();
Peeps[i].setMajor(Answer);
}

for (int i = 0; i < 3; i++) {
System.out.print(Peeps[i]); 
}

try {
  FileOutputStream fileOut = new FileOutputStream("Peeps.fhm");
  ObjectOutputStream out = new ObjectOutputStream(fileOut);
  out.writeObject(Peeps);
  out.close();
  fileOut.close();
  System.out.println("\nSerialization Successful\n");

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

try { 
  FileInputStream fileIn = new FileInputStream("Peeps.fhm");
  ObjectInputStream in = new ObjectInputStream(fileIn);
  System.out.println("Deserialized Data: \n" + in.readObject().toString());
  in.close();
  fileIn.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
}
}

我的教授希望我提示用户在终端输入两个 Person 对象的数据,然后将对两个填充的 Person 之一的引用复制到最后一个 Person。然后程序会将所有三个人写入扩展名为“.fhm”的磁盘文件(我已经完成)。我的问题是如何复制参考?我的第二个问题也是我如何正确反序列化文件,因为当我运行它时,它可以工作,但弹出的问题是它告诉我:

Deserialized Data: 
[LPerson;@5e481248

他希望它打印用户输入的信息。我检查了它写入的 fhm 文件,它收集了所有信息,所以我不确定我做错了什么。任何帮助将不胜感激,对不起,这篇文章有点长。提前致谢。

【问题讨论】:

    标签: java arrays serialization reference deserialization


    【解决方案1】:

    您的对象基本上是一个 Java 数组。要打印出来,您可以使用:

    System.out.println("Deserialized Data: \n" + Arrays.toString(in.readObject()));
    

    【讨论】:

      【解决方案2】:

      我不确定您所说的“复制参考”部分是什么意思。

      反序列化似乎运行良好。 但是在您的代码中,您实际上只是调用.toString() 来打印一个数组,这可能不是您想要的。 您可能想要遍历这些项目并一一打印它们:

        FileInputStream fileIn = new FileInputStream("Peeps.fhm");
        ObjectInputStream in = new ObjectInputStream(fileIn);
        Person[] peeps = in.readObject();
        System.out.println("Deserialized Data:");
        for (Person person : peeps) { 
            System.out.printf("First Name: %s MI: %s Last Name: %s%n", 
                person.getFname(), person.getMI(), person.getLname()); 
        }
      

      【讨论】:

      • 感谢您的澄清,但是当它反序列化时它仍然打印出“Person@4aa298b7Person@4b67cf4dPerson@610455d6”我必须组织它以便逐个打印出每个输入,例如:名字: MI: Last Name: Age: Gpa: Major: 我确定我必须输入 System.out.println("First Name: " _______) 但是那个空白部分会写什么?
      • 我更新了我的答案,调整成你喜欢的模式
      • 它似乎不喜欢增强的 for 循环,因为它最后给了我一个 NullPointerException 错误。它打印出输入的 3 个人信息,但在它给了我这个 java.lang.NullPointerException at TestPerson.main(TestPerson.java:73) 其余代码: for (Person person : Peeps) { System.out. printf("名字: %s MI: %s 姓: %s 年龄: %s GPA: %s 专业: %s%n", person.getFname(), person.getMI(), person.getLname() , person.getAge(), person.getGpa(), person.getMajor()); } in.close(); fileIn.close();
      • 程序的第 73 行是什么?
      • System.out.printf("First Name: %s MI: %s Last Name: %s Age: %s GPA: %s Major: %s%n", person.getFname(), person.getMI(), person.getLname(), person.getAge(), person.getGpa(), person.getMajor());
      猜你喜欢
      • 2015-06-12
      • 2021-04-22
      • 1970-01-01
      • 1970-01-01
      • 2021-08-14
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多