我的ArrayList只包含我的数组中的最后一项 [英] My ArrayList only contains the last item on my array

查看:73
本文介绍了我的ArrayList只包含我的数组中的最后一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;

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

I tried to display the current values of the variables above and compare them to the student object

    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只显示一个值:

Then I noticed my Arraylist only displays only one value:

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

通常我会读100 +项目,但这个例子我只做了5;
这是该计划的输出;

Normally I would read 100+ items but with this example I made it only 5; this is the out put of the program;

      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晚。

Now my problem here ladies and gentlemen is that I tried to display the Stud object inorder to check if its reading the correct values and indeed it displays it correctly, and then after assigning values to it , I then add it to the list. but something confuses me , and I couldnt figure out why my list only contains the last item in my array? please help me, I've been stuck for 2 nights.

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

this my whole code , I focus on the main

public class Converter {

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

}

推荐答案

问题是 Student 类中的字段是 static 。你没有显示代码,但可以猜到:

The problem is that your fields in the Student class are static. You haven't shown the code, but it could be guessed as this:

public class Student {

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

只需从此课程的字段中删除 static 标记。

Just remove the static marker from the fields in this class.

public class Student {

    //field must not be static
    private int id;

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

请注意,在<$ c的字段中删除 static 标记$ c>转换器类无法解决问题

Note that removing the static marker in the fields of Converter class won't fix the problem.

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

In short, static fields belong to the class, while non-static fields belong to the class object reference. If having static fields in your Student class, all the object references will share this value. When having non-static fields, since they belong to each instance of the class, every object reference will have a different value.

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

这篇关于我的ArrayList只包含我的数组中的最后一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆