java - 创建和访问 Vector 类

标签 java class vector

我正在尝试读取 .txt 文件的每一行并将其转换为 vector 格式。看来我的信息正在被转换,因为 vectPers.size() 给了我 21,这是我拥有的人数。 问题是,当我尝试打印 vector 时,它不断显示这些占 21 人的奇怪线条

Personne@15db9742
Personne@6d06d69c
Personne@7852e922
Personne@4e25154f
Personne@70dea4e
Personne@5c647e05
Personne@33909752
Personne@55f96302
Personne@3d4eac69
Personne@42a57993
Personne@75b84c92
Personne@6bc7c054
Personne@232204a1
Personne@4aa298b7
Personne@7d4991ad
Personne@28d93b30
Personne@1b6d3586
Personne@4554617c
Personne@74a14482
Personne@1540e19d
Personne@677327b6

这是我到目前为止的代码:

import java.io.*;
import java.util.Vector;


class Personne
{
  private String nomPre ;
  private double taille, poids;
  private char sexe ;
  private int numero;

  public Personne( String nomPre, char sexe, double taille, double poids, int numero) {
      this.nomPre = nomPre ;
      this.sexe = sexe;
      this.taille = taille ;
      this.poids = poids;
      this.numero = numero;
  }


    public double getTaille() { return taille; }
    public double getPoids() {return poids; }
    public String getNomPre() { return nomPre; }
    public char getSexe() { return sexe ; }
    public int getNumero() { return numero; }

    public void afficher()
    {
        System.out.printf("%5d %30s %6.2f %7.1f %s\n",
           numero, nomPre, taille, poids, sexe == 'F' ? "feminin":"masculin");
    }

}

public class numero3
{
    static int lireAfficher(String nomFichier)
        throws IOException
    {
      final int LONG_NP = 30, COL1 = 37, COL2 = 41,
                              COL3 = 51, COL4 = 56,
                              COL5 = 64, COL6 = 68;

      int n = 0;
      boolean existeFichier = true ; 

      FileReader fr = null; 


       try {
             fr = new FileReader (nomFichier) ;
       }

       catch ( java.io.FileNotFoundException erreur) {
            System.out.println("Probleme d'ouvrir le fichier " +
                 nomFichier);
            existeFichier = false ; 
       }

       if (existeFichier) {



          BufferedReader entree = new BufferedReader(fr);
          boolean finFichier = false ;

          while ( !finFichier ) {


           String uneLigne = entree.readLine();

           if (uneLigne == null)
                finFichier = true ;
           else {

                 String unNom = uneLigne.substring(0,LONG_NP);

                 char unSexe = uneLigne.charAt(LONG_NP);

                 double uneTaille =
                    ( new Double(uneLigne.substring(COL1, COL2).trim())).doubleValue();
                 double unPoids   =
                    ( new Double(uneLigne.substring(COL3, COL4).trim())).doubleValue();
                 int unNumero = Integer.parseInt(uneLigne.substring(COL5, COL6).trim());


                 n++;
                 Personne unePers = new Personne(unNom, unSexe, uneTaille, unPoids, unNumero);
                    unePers.afficher();

           }
          }
         entree.close();
        }
      return n;
    }


static int lireRemplir(String nomFichier,Personne[]pers,Vector<Personne>vectPers)
        throws IOException
    {
      final int LONG_NP = 30, COL1 = 37, COL2 = 41,
                              COL3 = 51, COL4 = 56,
                              COL5 = 64, COL6 = 68;

      int n = 0;
      boolean existeFichier = true ; 

      FileReader fr = null; 


       try {
             fr = new FileReader (nomFichier) ;
       }

       catch ( java.io.FileNotFoundException erreur) {
            System.out.println("Probleme d'ouvrir le fichier " +
                 nomFichier);
            existeFichier = false ; 
       }

       if (existeFichier) {


          BufferedReader entree = new BufferedReader(fr);
          boolean finFichier = false ;

          while ( !finFichier ) {


           String uneLigne = entree.readLine();

           if (uneLigne == null)
                finFichier = true ;
           else {

                 String unNom = uneLigne.substring(0,LONG_NP);

                 char unSexe = uneLigne.charAt(LONG_NP); 


                 double uneTaille =
                    ( new Double(uneLigne.substring(COL1, COL2).trim())).doubleValue();
                 double unPoids   =
                    ( new Double(uneLigne.substring(COL3, COL4).trim())).doubleValue();
                 int unNumero = Integer.parseInt(uneLigne.substring(COL5, COL6).trim());



                 pers[n++]= new Personne(unNom, unSexe, uneTaille, unPoids, unNumero);
                 vectPers.add(new Personne(unNom, unSexe,uneTaille,unPoids,unNumero));
           }
          }
         entree.close();
        }
      return n;
    }

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

        int nbPers = lireAfficher("met_a14.txt");
        System.out.printf("On vient de lire %d personnes\n", nbPers);

        Vector<Personne> vectPers = new Vector<Personne> ();

        final int MAX_PERS=nbPers;
        Personne[] pers=new Personne[MAX_PERS];
        int nbPerso = lireRemplir("met_a14.txt",pers,vectPers);

        for(int i=0;i<vectPers.size();i++){
        System.out.println(vectPers.get(i));

    }
}

我想知道是否是因为我需要创建一个像我的 Personne 类一样的 Vector 类。但找不到任何有关如何启动它的信息。

最佳答案

您应该重写 Person 类中的 toString() 方法以获得有意义的输出。打印的值来自 java.lang.Object 类中 toString() 的默认实现。

此页面位于 toString() JavaPractices 中应该为您提供更多信息。

关于java - 创建和访问 Vector 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26833508/

相关文章:

java - 与Windows台式机相比,Java在Linux服务器中的运行速度较慢

java - jlink :ed application throws exception中的记录

java - 带 https 的 Spring web 和 tomcat

java - 删除 JInternalFrame 标题栏中的点

r - 在向量中查找大于 X 的第一个值的位置

c++ - 使用 std::sort 对类 vector 进行降序排序

c++ - 在 boost.python 中;如何公开包含在另一个类中的类(通过组合)?

PHP - 作为静态数组元素的匿名函数

c++ - 段错误 : Core dumped C++ vector pairs of string:

c++ - 在 vector C++14(无限制 union )中的结构内的 union 中创建和存储字符串