java - My ArrayList only contains the last item on my array -
this question has answer here:
public static void main(string args[]){ new converter(); //the converter() method reads csv file pass array of string // called stringlist; in stringlist 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 display current values of variables above , compare them student object
system.out.println(id +" "+ yr +" "+ fname + " "+gname + " "+mname + " "+course +" should same : " +stud.tostring()); list.add(studs); // add student object list } //end of loop
then noticed arraylist displays 1 value:
(int c = 0; c<list.size(); c++){ system.out.println(" @list "+c+": "+list.get(c)); } } // end of main method
normally read 100+ items example made 5; out put of program;
2123259 1 avila jeremy raymund t bsit should same : 2123259,avila,jeremy raymund,t,bsit,1 2124919 1 beroÑa maynard w bsit should same : 2124919,beroÑa,maynard,w,bsit,1 2124679 1 cabrera jerson rhod d bsit should same : 2124679,cabrera,jerson rhod,d,bsit,1 2124905 1 cabrillas armando jr. b bsit should same : 2124905,cabrillas,armando jr.,b,bsit,1 2123400 1 cariÑo jvanz s bsit should same : 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
now problem here ladies , gentlemen tried display stud object inorder check if reading correct values , indeed displays correctly, , after assigning values , add list. confuses me , , couldnt figure out why list contains last item in array? please me, i've been stuck 2 nights.
this whole code , focus on main
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 same : " +stud.tostring()); list.add(stud); } // end of loop (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; }
}
the problem fields in student
class static
. haven't shown code, guessed this:
public class student { private static int id; //other fields... //constructor... //getters , setters... }
just remove static
marker fields in class.
public class student { //field must not static private int id; //other non-static fields... //constructor... //getters , setters... }
note removing static
marker in fields of converter
class won't fix problem.
in short, static
fields belong class, while non-static
fields belong class object reference. if having static
fields in student
class, object references share value. when having non-static
fields, since belong each instance of class, every object reference will have different value.
more info: understanding instance , class members
Comments
Post a Comment