i have this sample list from a file
firstname, lastname, age
anne, smith, 6
dougie, miller, 8
right now i am stuck with the arraylist
ArrayList<String[]> rows = new ArrayList<>();
while ((line = reader.readLine()) != null) {
String[] row = line.split(",");
rows.add(row);
}
for (String[] row : rows) {
System.out.println(Arrays.toString(row));
}
and i got these output:
[firstname,lastname,age]
[anne,smith,6]
[dougie,miller,8]
i have tried using doing this:
String[] x1 = new String[rows.size()+1];
for(int i=0;i<rows.size();i++){
String[] lastname = (String[]) rows.get(i+1);
System.out.println(lastname[1]);
//x1[i]=lastname[1];
}
but i couldn’t seem to put the lastname in one array. it will give me this error:
Exception in thread “AWT-EventQueue-0” java.lang.IndexOutOfBoundsException: Index: 227, Size: 227
i want to have this output:
firstname anne dougie lastname smith miller age 6 8
how do i do that?
This one should work fine, provided the StringArrays are all same sized: