Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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; i.e.:

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
like image 474
CaptainProg Avatar asked Jan 30 '26 15:01

CaptainProg


1 Answers

You would probably be better served by using a Map data structure (e.g. 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);
like image 146
Scott W Avatar answered Feb 01 '26 04:02

Scott W



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!