如何编写一个方法(分解)将字符串和 returns 组成 java 中的字符串的字符数组作为输入?

how to write a method (decompose) that takes as input a string and returns the array of characters that compose the string in java?

提示:

表达式s.charAt(i) returns字符串s中位置i的字符

测试:

String word = "Hello";
System.out.println("The word \""+word+"\" decomposes as "+Arrays.toString(decomposeString(word)));

结果:

单词“Hello”分解为[h, e, l, l, o]

您可以使用 .toCharArray() 方法。

例子

String str = "Hello";
char[] ch = str.toCharArray();

More information