how to add data to ARRAYLIST
Posted
by
Chamal
on Stack Overflow
See other posts from Stack Overflow
or by Chamal
Published on 2011-01-01T06:32:52Z
Indexed on
2011/01/01
6:54 UTC
Read the original article
Hit count: 300
java
try {
ArrayList ar=new ArrayList();
PRIvariable pri=new PRIvariable();
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("C:/cdr2.csv")));
while (reader.ready()) {
String line = reader.readLine();
String[] values = line.split(",");
pri.dateText=values[2]+" "+values[4];
pri.count=pri.count+1;
pri.sum = pri.sum+Integer.parseInt(values[7]);
System.out.println(pri.dateText+" "+pri.sum+" "+pri.count);
ar.add(pri);
}
String[] columnNames={"Date","TOTAL","COUNTS"};
String[][] cells=new String[ar.size()][3];
for(int i=0;i<ar.size();i++){
cells[i][0]=((PRIvariable)ar.get(i)).dateText;
cells[i][1]=""+((PRIvariable)ar.get(i)).sum;
cells[i][2]=""+((PRIvariable)ar.get(i)).count;
}
table = new JTable(cells,columnNames);
table.setSize(400,400);
table.setVisible(true);
JScrollPane js=new JScrollPane();
js.setViewportView(table);
js.setSize(400,400);
js.setVisible(true);
add(js,java.awt.BorderLayout.CENTER);
} catch (Exception e) {
System.out.println(e);
}
This is my code. Here i want to Read text file and put that data to Jtable. But in this code it shows every row of the Jtable filled with same data that contain in arraylist(ar) last row. ( i think there is problem in my arraylist). How can i solve this......
© Stack Overflow or respective owner