gpt4 book ai didi

java - 将 List 转换为文本数组 MapReduce 时出现 ArrayStoreException

转载 作者:行者123 更新时间:2023-11-30 08:06:35 25 4
gpt4 key购买 nike

在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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com