我目前有一个包含5列的多维数组,通过另一个数组(名称数组)中的句子填充其元素我的问题是所有内容都存储在第一列中。例如,
word[][] = [25 Jackson 11,693 Nevaeh 6,345] [] [] [] []
但是我希望它像这样存储
word[][] = [25][Jackson][11,693][Nevaeh][6,455]
String [] names = topNames.toArray(new String[topNames.size()]);
String[][] words = new String[names.length][5];
for (int i = 0; i < names.length; i++){
for (int j = 0; j < names[i].length(); j++){
words[i][0] = names[i];
}
}
for (int i = 0; i < words.length; i++){
System.out.println(words[i][0]);
}
答案 0 :(得分:2)
你的意思是这样的吗?
String[] names = new String[]{
"25 Jackson 11,693 Nevaeh 6,345",
"26 Jackson 44,444 Nevaeh 3,56"
};
String[][] words = new String[names.length][5];
for (int i = 0; i < names.length; i++) {
words[i] = names[i].split("\\s+");
}
Arrays.stream(words).map(Arrays::toString).forEach(System.out::println);
为什么不使用Collections
而不是数组?
List<String> names = Arrays.asList(
"25 Jackson 11,693 Nevaeh 6,345",
"26 Jackson 44,444 Nevaeh 3,56"
);
List<List<String>> words = names.stream()
.map(sentence -> sentence.split("\\s+"))
.map(Arrays::asList)
.collect(Collectors.toList());
words.forEach(System.out::println);