如何将存储在字符串数组中的特定字符串打印到字符串数组的 ArrayList 中?
How to print a specific String stored into an array of String into an ArrayList of Strings Arrays?
所以,根据问题,我在 Java 中有这段代码:
public class example {
static ArrayList<String[]> test = new ArrayList<String[]>();
private String[] a = {"this", "is,", "a test"};
private String[] b = {"Look", "a three-headed", "monkey"};
public void fillTest() {
test.add(a);
test.add(b);
// so far so good, I checked this method
// with a System.out.print and it works
}
// later in the code I have a method that try
// to take the arrayList test and copy it into
// a String[] named temp. In my vision temp
// should than be accessed randomly by the
// method itself and the content printed out
// from temp should be removed from test -
// that's why I'm using an ArrayList
public void stuff() {
// some stuff
// runtime error happens here:
String[] temp = test.toArray(new String[test.size()]);
// other stuff that never made it to runtime
}
}
问题是虽然编译器对此没有任何反对意见,但在运行时我收到此错误:
Exception in thread "main" java.lang.ArrayStoreException: arraycopy: element type mismatch: can not cast one of the elements of java.lang.Object[] to the type of the destination array, java.lang.String
我不明白背后的原因 - 在我看来我是在要求它用字符串填充字符串数组,那么为什么会出错?
您正在尝试将元素为 String
数组的 List
转换为元素为 String
的数组。这不起作用,因为 String
的数组不是 String
.
相反,您可以将 List
个数组转换为 String
个二维数组:
String[][] temp = test.toArray(new String[test.size()][]);
如果想把List
的String数组的所有元素放在一个String
的数组中,就得做一些处理。使用 Stream
s 可以完成:
String[] temp = test.stream().flatMap(Arrays::stream).toArray(String[]::new);
您可以使用 Stream.flatMap
方法展平此 字符串数组列表 并在单个字符串数组上获取流。然后你可以得到一个包含这个流的元素的数组:
List<String[]> test = Arrays.asList(
new String[]{"this", "is,", "a test"},
new String[]{"Look", "a three-headed", "monkey"});
String[] temp = test
// return Stream<String[]>
.stream()
// return Stream<String>
.flatMap(arr -> Arrays.stream(arr))
// return an array of a specified size
.toArray(size -> new String[size]);
System.out.println(Arrays.toString(temp));
// [this, is,, a test, Look, a three-headed, monkey]
另请参阅:
所以,根据问题,我在 Java 中有这段代码:
public class example {
static ArrayList<String[]> test = new ArrayList<String[]>();
private String[] a = {"this", "is,", "a test"};
private String[] b = {"Look", "a three-headed", "monkey"};
public void fillTest() {
test.add(a);
test.add(b);
// so far so good, I checked this method
// with a System.out.print and it works
}
// later in the code I have a method that try
// to take the arrayList test and copy it into
// a String[] named temp. In my vision temp
// should than be accessed randomly by the
// method itself and the content printed out
// from temp should be removed from test -
// that's why I'm using an ArrayList
public void stuff() {
// some stuff
// runtime error happens here:
String[] temp = test.toArray(new String[test.size()]);
// other stuff that never made it to runtime
}
}
问题是虽然编译器对此没有任何反对意见,但在运行时我收到此错误:
Exception in thread "main" java.lang.ArrayStoreException: arraycopy: element type mismatch: can not cast one of the elements of java.lang.Object[] to the type of the destination array, java.lang.String
我不明白背后的原因 - 在我看来我是在要求它用字符串填充字符串数组,那么为什么会出错?
您正在尝试将元素为 String
数组的 List
转换为元素为 String
的数组。这不起作用,因为 String
的数组不是 String
.
相反,您可以将 List
个数组转换为 String
个二维数组:
String[][] temp = test.toArray(new String[test.size()][]);
如果想把List
的String数组的所有元素放在一个String
的数组中,就得做一些处理。使用 Stream
s 可以完成:
String[] temp = test.stream().flatMap(Arrays::stream).toArray(String[]::new);
您可以使用 Stream.flatMap
方法展平此 字符串数组列表 并在单个字符串数组上获取流。然后你可以得到一个包含这个流的元素的数组:
List<String[]> test = Arrays.asList(
new String[]{"this", "is,", "a test"},
new String[]{"Look", "a three-headed", "monkey"});
String[] temp = test
// return Stream<String[]>
.stream()
// return Stream<String>
.flatMap(arr -> Arrays.stream(arr))
// return an array of a specified size
.toArray(size -> new String[size]);
System.out.println(Arrays.toString(temp));
// [this, is,, a test, Look, a three-headed, monkey]
另请参阅: