How to add multiple characters to one index in a Char Array?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to add multiple characters to one index in a Char Array?



Im currently trying to create a function where my input is a string such as "AABBCCDDEE" and the function outputs a String array "AA""BB""CC" and so on.



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(" ");






So you just want to find "runs?" Sequences of contiguous characters that are the same?
– markspace
3 mins ago





Yes, and each run should be in a seperate index
– air bmx
2 mins ago





You realize you can't store *multiple* characters at a single index, right? You'll need to use a two dimensional char array, a string, or something else.
– markspace
1 min ago




1 Answer
1



Arrays are fixed-length, so you can't do this with an array unless you allocate one with sufficient extra room up-front (which would require a pass through the string to find out how much extra room you needed).



Instead, consider using a StringBuilder for the output, which you can convert into a char array when you're done.


StringBuilder


char





How do you convert from StringBuilder to chararray. Is there a .toCharArray method?
– air bmx
20 secs ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS