如何从文本文件创建对象数组?

How can I create an Array of Objects from a text file?

我正在尝试完成我目前正在做的 Java class 的作业,但是我在创建对象数组(在本例中为长颈鹿)时遇到了一些问题一个文本文件。谁能给我一些建议?

我遇到的问题是,当我尝试打印 ArrayList 或转换后的数组的值时,我得到的是 [giraffe.Giraffe2@55f96302, giraffe.Giraffe2@3d4eac69] 之类的东西,而不是文本中的值文件。

我用多种不同的方式创建了一个名为 Giraffes 的 class,最有意义的似乎是使用数组来存储构造函数的值并设置数组中的值。

package giraffe;

public class Giraffe2 {
protected String birthLocation, sire, dam, subSpecies, zoo, city,
state,event, name,
localId, birthDate, sex;
protected int giraffeId;
public void Giraffe2(String array[]){
    this.giraffeId = array[0];
    this.sex = array[1];
    this.birthDate = array[2];
    this.sire = array[3];
    this.dam = array[4];
    this.birthLocation = array[5];
    this.localId = array[6];
    this.name = array[7];
    this.subSpecies = array[8];
    this.zoo = array[9];
    this.city = array[10];
    this.state = array[11];
    this.event = array[12];
 }
}

我用来创建长颈鹿的代码是:

package giraffe;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class HW3 {

public static void main(String[] args) throws IOException {
    //Create an ArrayList that will hold the Giraffes
    ArrayList<Giraffe2> giraffes = new ArrayList<>();

    //Create an array that will hold tab-del values
    String temp[] = new String[13];

    //Define the file that will be used for creating objects
    String fileLocation = "theHerd.txt";
    File textFile = new File(fileLocation);

    //Populate the giraffes ArrayList with items from the text file
    int i = 0;
    while(i < 90) {
    if (textFile.canRead()) {
        Scanner in = new Scanner(textFile);
        in = new Scanner(textFile);
        while (in.hasNextLine()) {
    //Split each line at every tab (13 times), store in array temp
            temp = in.nextLine().split("\t", 13);
    //Create a new giraffe from the array of strings in temp
            giraffes.add(new Giraffe2(temp));
            i++;
        }

        in.close();
    }
    //Print out ArrayList of giraffes
    System.out.println(giraffes);
    //Convert ArrayList to array
    Object[] giraffes2 = giraffes.toArray();
    //Print out a few examples
    System.out.println(giraffes2[0]);
    System.out.println(giraffes2[1]);
    System.out.println(giraffes2[89]);
    }
 }
}

这段代码的输出是:

[giraffe.Giraffe2@55f96302, giraffe.Giraffe2@3d4eac69, giraffe.Giraffe2@42a57993, giraffe.Giraffe2@75b84c92, giraffe.Giraffe2@6bc7c054, giraffe.Giraffe2@232204a1, giraffe.Giraffe2@4aa298b7, giraffe.Giraffe2@7d4991ad, giraffe.Giraffe2@28d93b30, giraffe.Giraffe2@1b6d3586, giraffe.Giraffe2@4554617c, giraffe.Giraffe2@74a14482, giraffe.Giraffe2@1540e19d, giraffe.Giraffe2@677327b6, giraffe.Giraffe2@14ae5a5, giraffe.Giraffe2@7f31245a, giraffe.Giraffe2@6d6f6e28, giraffe.Giraffe2@135fbaa4, giraffe.Giraffe2@45ee12a7, giraffe.Giraffe2@330bedb4, giraffe.Giraffe2@2503dbd3, giraffe.Giraffe2@4b67cf4d, giraffe.Giraffe2@7ea987ac, giraffe.Giraffe2@12a3a380, giraffe.Giraffe2@29453f44, giraffe.Giraffe2@5cad8086, giraffe.Giraffe2@6e0be858, giraffe.Giraffe2@61bbe9ba, giraffe.Giraffe2@610455d6, giraffe.Giraffe2@511d50c0, giraffe.Giraffe2@60e53b93, giraffe.Giraffe2@5e2de80c, giraffe.Giraffe2@1d44bcfa, giraffe.Giraffe2@266474c2, giraffe.Giraffe2@6f94fa3e, giraffe.Giraffe2@5e481248, giraffe.Giraffe2@66d3c617, giraffe.Giraffe2@63947c6b, giraffe.Giraffe2@2b193f2d, giraffe.Giraffe2@355da254, giraffe.Giraffe2@4dc63996, giraffe.Giraffe2@d716361, giraffe.Giraffe2@6ff3c5b5, giraffe.Giraffe2@3764951d, giraffe.Giraffe2@4b1210ee, giraffe.Giraffe2@4d7e1886, giraffe.Giraffe2@3cd1a2f1, giraffe.Giraffe2@2f0e140b, giraffe.Giraffe2@7440e464, giraffe.Giraffe2@49476842, giraffe.Giraffe2@78308db1, giraffe.Giraffe2@27c170f0, giraffe.Giraffe2@5451c3a8, giraffe.Giraffe2@2626b418, giraffe.Giraffe2@5a07e868, giraffe.Giraffe2@76ed5528, giraffe.Giraffe2@2c7b84de, giraffe.Giraffe2@3fee733d, giraffe.Giraffe2@5acf9800, giraffe.Giraffe2@4617c264, giraffe.Giraffe2@36baf30c, giraffe.Giraffe2@7a81197d, giraffe.Giraffe2@5ca881b5, giraffe.Giraffe2@24d46ca6, giraffe.Giraffe2@4517d9a3, giraffe.Giraffe2@372f7a8d, giraffe.Giraffe2@2f92e0f4, giraffe.Giraffe2@28a418fc, giraffe.Giraffe2@5305068a, giraffe.Giraffe2@1f32e575, giraffe.Giraffe2@279f2327, giraffe.Giraffe2@2ff4acd0, giraffe.Giraffe2@54bedef2, giraffe.Giraffe2@5caf905d, giraffe.Giraffe2@27716f4, giraffe.Giraffe2@8efb846, giraffe.Giraffe2@2a84aee7, giraffe.Giraffe2@a09ee92, giraffe.Giraffe2@30f39991, giraffe.Giraffe2@452b3a41, giraffe.Giraffe2@4a574795, giraffe.Giraffe2@f6f4d33, giraffe.Giraffe2@23fc625e, giraffe.Giraffe2@3f99bd52, giraffe.Giraffe2@4f023edb, giraffe.Giraffe2@3a71f4dd, giraffe.Giraffe2@7adf9f5f, giraffe.Giraffe2@85ede7b, giraffe.Giraffe2@5674cd4d, giraffe.Giraffe2@63961c42]

giraffe.Giraffe2@55f96302
giraffe.Giraffe2@3d4eac69
giraffe.Giraffe2@63961c42

非常感谢任何帮助。我已经和这个问题斗争了几天了,我真的很难理解问题是什么

为了使用 System.out.println 打印一个对象,您需要实现要打印的 class 的 toString() 方法。

为了正确打印 class 个对象的内容,包含这些对象的 class 必须实现方法 toString():

public String toString () { 

   return // here write the results as you want them to appear on the screen;    
}

您可以在调用或不调用 toString() 方法的情况下打印对象,即无论您使用 System.out.print(objectName.toString) 还是 System.out.print(对象名称)。在实现上述方法 toString() 之后,您只需要将此 class 的对象名称作为参数放入方法 System.out.print(String s) 中, 例如:

public class Birdwatcher {

// the Array List in this class contains as its objects the elements of class Bird

       private ArrayList<Bird> birds;

    // here you write your other code 
}
    ...........................................................................................     

public class Bird { 

// objects of this class will be saved as an Array List in class Birdwatcher 

     private String name;
     private String latinName;

     public Bird (String name, String latinName){
       this.name = name;
       this.latinName = latinName;
     }

   // here you write your other code 

      public String toString() {

        // this is the required method in order to avoid the printing errors 

           return name + ", " + latinName;
      }
}

    ................................................................................



public class MainClass{

    public static void main(String[] args) {

       Birdwatcher birdwatcher = new Birdwatcher();

       // here you write your other code

       for (Bird allBirds : birdwatcher.birds()) {

           System.out.println(allBirds);
         // or  System.out.println(allBirds.toString);
       }
}