【发布时间】:2017-03-16 12:21:23
【问题描述】:
我有数组
String[] test_=new String[] {"a b c d", "f g h i","j k l s gf"};
现在我想创建另一个包含元素的数组
{"b d", "g i","k s"}
我该怎么做?
我已经设法将数组分隔成行使用
String split_test[] = null;
for (int j = 0 ; j <= 2 ; j++) {
split_test=test_[j].split("\\s+");
System.out.println(Arrays.toString(split_test));
}
但现在我想分开每一行,我尝试了解决方案 How to Fill a 2d array with a 1d array? 结合类似这样的 split_test=test_[j].split("\s+"),但是我一直没能解决。
另外,如果我按照他们说的做,我必须让数组 split_test 有许多特定的列,但我想要的是 split_test 的列的大小取决于数组 test_。例如,如果我想要一个包含元素 {"b d", "g i", "k s gf"}
String[][] split_test = new String[3][2];
for(int row = 0; row < split_test.length; row++) {
for(int col = 0; col < split_test[row].length; col++) {
split_test[row][col] = test_[row];/*I still don't understand how to use the split within the for*/
System.out.println(split_test[row][col]);
}
}
有没有更简单有效的方法?
谢谢
【问题讨论】:
-
你想在
test_上添加什么逻辑来从数组中读取项目?
标签: java arrays string vector split