【发布时间】:2018-09-24 05:04:47
【问题描述】:
我目前正在尝试创建一个函数,其中我的输入是一个字符串,例如“AABBCCDDEE”,该函数输出一个字符串数组“AA”“BB”“CC”等等。
public static char[] stringSplitter(final String input) {
String[] strarray = new String[input.length()];
if (input == null) {
return null;
}
char[] chrarray = input.toCharArray();
char[] outputarray = new char[input.length()];
int j = 0;
for (int i = 0; i < chrarray.length; i++) {
char chr = chrarray[i];
System.out.print(chr);
outputarray[j] = chrarray[i]; //here i need to find a way to add the characters to the index at j if the preceding characters are equal
if (i + 1 < input.length() && chrarray[i + 1] != chr) {
j++;
outputarray[j] = chrarray[i + 1];
System.out.println(" ");
}
}
}
【问题讨论】:
-
所以你只想找到“runs”?相同的连续字符序列?
-
是的,每次运行都应该在一个单独的索引中
-
您意识到您不能在单个索引中存储 *multiple* 个字符,对吧?您需要使用二维字符数组、字符串或其他内容。
-
我不知道,谢谢你的澄清。这意味着我只能使用字符串数组
标签: java arrays string character