在mapreduce代码中,将列表转换为(org.apache.hadoop.io.Text)文本类型的数组时,收到ArrayStoreException。
List<String> testList= new ArrayList<String>();
testList.add("testData1");
testList.add("testData2");
Text[] testArray=testList.toArray(new Text[testList.size()]);
但是当我不向列表添加任何值然后将其转换为数组时,它工作正常(使用空值)。有人可以指出我的错误吗?
最佳答案
您无法存储String
位于 Text
数组(Text[]
)。
您可以将它们存储在 String
中数组(String[]
):
String[] testArray=testList.toArray(new String[testList.size()]);
when i dont add any value to the list and then convert it to array, it works fine
它仅在本例中创建空数组时才有效,因此其中不会存储任何内容。
如果您必须出示Text[]
包含来自源 List<String>
的数据,您必须迭代 List 并自己生成 Text 实例:
Text[] testArray = new Text[testList.size()];
for (int i = 0; i < testList.size(); i++) {
testArray[i] = new Text(testList.get(i)); // assuming the Text class
// has such a constructor
}
关于java - 将 List<String> 转换为文本数组 MapReduce 时出现 ArrayStoreException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31006298/