我的ArrayList只包含我的数组中的最后一项

时间:2013-07-10 22:05:48

标签: java

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对象,以检查它是否正确读取值并确实正确显示,然后在为其分配值后,我将其添加到列表中。但有些东西让我困惑,我无法弄清楚为什么我的列表只包含我的数组中的最后一项?请帮助我,我被困了2个晚上。

这是我的整个代码,我专注于主要的

public class Converter {

    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;
    }

}

2 个答案:

答案 0 :(得分: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字段时,由于它们属于该类的每个实例,因此每个对象引用具有不同的值。

更多信息:Understanding Instance and Class Members

答案 1 :(得分:1)

您在每次循环迭代时不断覆盖变量idyrfname,...这些变量也可能直接用作Student类中的字段。尝试在每次循环迭代时使用 fresh 变量。

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