简体   繁体   中英

Transposing arrays

I am using the following code to read in a CSV file:

  String next[] = {};
  List<String[]> dataArray = new ArrayList<String[]>();

  try {
      CSVReader reader = new CSVReader(new InputStreamReader(getAssets().open("inputFile.csv")));
      for(;;) {
          next = reader.readNext();
          if(next != null) {
              dataArray.add(next);
          } else {
              break;
          }
      }
  } catch (IOException e) {
      e.printStackTrace();
  }

This turns a CSV file into the array 'dataArray'. My application is for a dictionary type app - the input data's first column is a list of words, and the second column is the definitions of those words. Here is an example of the array loaded in:

Term 1, Definition 1
Term 2, Definition 2
Term 3, Definition 3

In order to access one of the strings in the array, I use the following code:

dataArray.get(rowNumber)[columnNumber]

However, I need to be able to generate a list of all the terms, so that they can be displayed for the dictionary application. As I understand it, accessing the columns by themselves is a much more lengthy process than accessing the rows (I come from a MATLAB background, where this would be simple).

It seems that in order to have ready access to any row of my input data, I would be better off transposing the data and reading it in that way; ie:

Term 1, Term 2, Term3
Definition 1, Definition 2, Definition 3

Of course, I could just provide a CSV file that is transposed in the first place - but Excel or OO Calc don't allow more than 256 rows, and my dictionary contains around 2000 terms.

Any of the following solutions would be welcomed:

  • A way to transpose an array once it has been read in
  • An alteration to the code posted above, such that it reads in data in the 'transposed' way
  • A simple way to read an entire column of an array as a whole

You would probably be better served by using a Map data structure (eg HashMap ):

String next[] = {};
HashMap<String, String> dataMap = new HashMap<String, String>();

try {
    CSVReader reader = new CSVReader(new InputStreamReader(getAssets().open("inputFile.csv")));
    for(;;) {
        next = reader.readNext();
        if(next != null) {
            dataMap.put(next[0], next[1]);
        } else {
            break;
        }
    }
} catch (IOException e) {
    e.printStackTrace();
}

Then you can access the first column by

dataMap.keySet();

and the second column by

dataMap.values();

Note one assumption here: that the first column of your input data is all unique values (that is, there are not repeated values in the "Term" column).

To be able to access the keys (terms) as an array, you can simply do as follows:

String[] terms = new String[dataMap.keySet().size()];
terms = dataMap.keySet().toArray(terms);

If each row has two values, where the first one is the term and the second one is the definition, you could build a Map of it like this (Btw, this while loop does the exact same thing as your for loop):

String next[] = {};
Map<String, String> dataMap = new HashMap<String, String>();

try {
    CSVReader reader = new CSVReader(new InputStreamReader(getAssets().open("inputFile.csv")));
    while((next = reader.readNext()) != null) {
        dataMap.put(next[0], next[1]);
    }
} catch (IOException e) {
    e.printStackTrace();
}

Then you can get the definition from a term via:

String definition = dataMap.get(term);

or all definitions like this:

for (String term: dataMap.keySet()) {
    String definition = dataMap.get(term);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM