what would be the advantages (or limitations) of using a 2d array instead of multiple 1d arrays in this instance


int maxQuestions = 3; int questionCount; String[][] data = new String[maxQuestions][8]; // above outside method -- below inside method int count, field; for (count = 0; count < maxQuestions; count++) { line = in.readLine(); if (line == null) break; for (field = 0; field < 7; field++) { if (line.indexOf(',') < 0) break; data[count][field] = line.substring(0, line.indexOf(',')); line = line.substring(line.indexOf(',') + 1); } data[count][field] = line; } questionCount = count;
/* * I assume you've created your BufferedReader and called it "in" * This also instead of just reading 3 questions, it reads however * many questions there are in the file. */ import java.util.List; import java.util.ArrayList; List<String> question = new ArrayList<String>(), answer_a = new ArrayList<String>(), answer_b = new ArrayList<String>(), answer_c = new ArrayList<String>(), answer_d = new ArrayList<String>(), answer_correct = new ArrayList<String>(); String line; while((line = in.readLine()) != null) { try { String[] data = line.split(","); question.add(data[0]); answer_a.add(data[1]); answer_b.add(data[2]); answer_c.add(data[3]); answer_d.add(data[4]); answer_correct.add(data[5]); } catch(Exception e) {}} //If you really need it in arrays: String[] q = question.toArray(), a = answer_a.toArray(), //etc
// the following variables are instance variables // they are declared in the class but outside of any methods int maxQuestions = 3; //maximum possible number of questions int questionCount; // the final total number of questions read in String[][] data = new String[maxQuestions][8]; // the 2D array of Strings will hold all the data read in // the number inside the first [ ] is the question number // the number inside the second [ ] is the field number // The following code goes inside of a method int count, field; // since these will continue to be used outside the for loop, they are declared first // (the 'int field;' could be declared before the inside for loop) for (count = 0; count < maxQuestions; count++) // read in the file, one line at a time { line = in.readLine(); // reads a Line into 'line' if (line == null) break; // if no lines left exit for loop for (field = 0; field < 7; field++) // break the line down, one field at a time { if (line.indexOf(',') < 0) break; // if no seperators left, exit for loop (only one field left) data[count][field] = line.substring(0, line.indexOf(',')); // save first field of line line = line.substring(line.indexOf(',') + 1); // remove first field from line } data[count][field] = line; // save last field of line } questionCount = count; // save total questions read in