-1
public static void main(String args[]){


            new Converter(); 
//the converter()  method reads a csv file that pass it to an array of String
// called stringList;  where in stringList is a  static variable;

    ArrayList<Student> list = new ArrayList<Student>(5);
    String p[] = null;
    Student stud = null;
    for(int z = 0; z < stringList.length; z++){
        p = stringList[z].split(",");

        id = Integer.parseInt(p[0]);

        yr = Integer.parseInt(p[5]);

        fname = p[1];

        gname = p[2];

        mname = p[3].charAt(0);

        course = p[4];

        stud = new Student(id,yr,fname,gname,mname,course);
        studs = stud;

我试图显示上面变量的当前值并将它们与学生对象进行比较

    System.out.println(id +" "+ yr +" "+ fname + " "+gname + " "+mname + " "+course +"  should be the same with : " +stud.toString());
        list.add(studs);    // add the student object to the list

    }  //end of the for loop

然后我注意到我的 Arraylist 只显示一个值:

    for (int c = 0; c<list.size(); c++){
        System.out.println(" @list "+c+": "+list.get(c));
    }
} // end of main method

通常我会阅读 100 多个项目,但在这个例子中我只读了 5 个;这是程序的输出;

      2123259 1 AVILA JEREMY RAYMUND T BSIT should be the same with :    2123259,AVILA,JEREMY RAYMUND,T,BSIT,1
      2124919 1 BEROÑA MAYNARD W BSIT should be the same with : 2124919,BEROÑA,MAYNARD,W,BSIT,1
      2124679 1 CABRERA JERSON RHOD D BSIT should be the same with :      2124679,CABRERA,JERSON RHOD,D,BSIT,1
      2124905 1 CABRILLAS ARMANDO JR. B BSIT should be the same with : 2124905,CABRILLAS,ARMANDO JR.,B,BSIT,1
      2123400 1 CARIÑO JVANZ S BSIT should be the same with : 2123400,CARIÑO,JVANZ,S,BSIT,1

      @list 0: 2123400,CARIÑO,JVANZ,S,BSIT,1
      @list 1: 2123400,CARIÑO,JVANZ,S,BSIT,1
      @list 2: 2123400,CARIÑO,JVANZ,S,BSIT,1
      @list 3: 2123400,CARIÑO,JVANZ,S,BSIT,1
      @list 4: 2123400,CARIÑO,JVANZ,S,BSIT,1

现在,女士们先生们,我的问题是我试图显示 Stud 对象以检查它是否读取正确的值并且确实正确显示它,然后在为其分配值之后,然后将其添加到列表中。但是有些事情让我感到困惑,我无法弄清楚为什么我的列表只包含数组中的最后一项?请帮帮我,我被困了两个晚上。

这是我的全部代码,我专注于主要

公共类转换器{

    private static ArrayList<Student> students;
    private static String[] stringList;
    private static Scanner fr;
    private static int size;
    private static int id;
    private static int yr;
    private static String fname;
    private static String gname;
    private static char mname;
    private static String course;


public static void main(String args[]){
    ArrayList<Student> list = new ArrayList<Student>(5);
    new Converter();
    String p[] = null;
    Student stud = null;


    students = new ArrayList<Student>(stringList.length);
    for(int z = 0; z < stringList.length; z++){
        p = stringList[z].split(",");
        id = Integer.parseInt(p[0]);
        yr = Integer.parseInt(p[5]);
        fname = p[1];
        gname = p[2];
        mname = p[3].charAt(0);
        course = p[4];
        stud = new Student(id,yr,fname,gname,mname,course);

        System.out.println(id +" "+ yr +" "+ fname + " "+gname + " "+mname + " "+course +" should be the same with : " +stud.toString());
        list.add(stud);

    }  // end of for loop
    for (int c = 0; c<list.size(); c++){
        System.out.println(" @list "+c+": "+list.get(c));
    } // end of second loop
}// end of main
public Converter(){

    readStudtentsCSV();
    id = 0;
    yr = 0;
    fname = "";
    gname = "";
    mname = ' ';
    course = "";
}
 public static void readStudtentsCSV() {
        int s = 0;
        try {
            size = countLines("test.csv");
            stringList = new String[size];
            fr = new Scanner(new File("test.csv"));  
        } catch(Exception e){
            e.printStackTrace();
        }

        while (fr.hasNextLine()){
            stringList[s] = fr.nextLine();
            //System.out.println(stringList[s]);
            s++;

        }
    }
 public static int countLines(String eFile) throws Exception{

        Scanner fScan = new Scanner(new File(eFile));
        int count =0;
        while (fScan.hasNextLine()){
        count += 1;
        String line = fScan.nextLine();
        }
        fScan.close();
        return count;
    }

}

4

2 回答 2

5

问题是您在Student课堂上的字段是static. 您还没有显示代码,但可以这样猜测:

public class Student {

    private static int id;
    //other fields...
    //constructor...
    //getters and setters...
}

只需static从此类中的字段中删除标记即可。

public class Student {

    //field must not be static
    private int id;

    //other non-static fields...
    //constructor...
    //getters and setters...
}

请注意,删除类static字段中的标记不会解决问题Converter

简而言之,static字段属于类,而非static字段属于类对象引用。如果static您的类中有字段Student,则所有对象引用都将共享此值。当有非static字段时,由于它们属于类的每个实例,每个对象引用具有不同的值。

更多信息:了解实例和类成员

于 2013-07-10T22:45:49.117 回答
1

您在每次循环迭代中不断地覆盖变量id, yr, fname, ... 。这些变量可能也直接用作Student类中的字段。尝试在每次循环迭代中使用一个新变量。

for(int z = 0; z < stringList.length; z++){
    p = stringList[z].split(",");

    int id = Integer.parseInt(p[0]);

    int yr = Integer.parseInt(p[5]);

    String fname = p[1];

    String gname = p[2];

    char mname = p[3].charAt(0);

    String course = p[4];

    Student stud = new Student(id,yr,fname,gname,mname,course);
    System.out.println(id +" "+ yr +" "+ fname + " "+gname + " "+mname + " "+course +"  should be the same with : " +stud.toString());
    list.add(stud);    // add the student object to the list

}  //end of the for loop
于 2013-07-10T22:21:56.533 回答