简体   繁体   English

转置数组

[英]Transposing arrays

I am using the following code to read in a CSV file: 我使用以下代码读取CSV文件:

  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'. 这会将CSV文件转换为数组'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). 据我所知,访问列是一个比访问行更长的过程(我来自MATLAB背景,这将是简单的)。

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. 当然,我可以提供一个首先转置的CSV文件 - 但Excel或OO Calc不允许超过256行,而我的词典包含大约2000个术语。

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 ): 使用Map数据结构(例如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). 请注意这里的一个假设:输入数据的第一列是所有唯一值(即“Term”列中没有重复的值)。

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): 如果每一行都有两个值,第一个是术语,第二个是定义,你可以像这样构建一个Map (顺便说一句,这个while循环与你的for循环完全相同):

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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