0

Essentially I have an array named symbols String[] symbols = {"a*", "j^", "y!"} which from that I would like to create another array with each of the single characters from the symbols array being in a single element in the new array, something like this char[] newArr = {'a', '*' ..... }.

In terms me solving this my guess would be to iterate over the elements in the first array then in a nested loop loop over the single characters in that element then assign it to the new array there, but I am unsure how to do that.

6
  • Iterate the symbols. With each entry, iterate over each char and add that char to the newArr array Commented Jan 13, 2015 at 20:24
  • 2
    Do you need the result as String[]? It seems to me that a char[] would be more useful here. Commented Jan 13, 2015 at 20:24
  • oh yes sorry missed that will edit Commented Jan 13, 2015 at 20:25
  • 1
    Input can't be a char[] as it has two characters together Commented Jan 13, 2015 at 20:26
  • 2
    @GeorgeSmith That's illegal code. Input should be String[]. Commented Jan 13, 2015 at 20:27

3 Answers 3

1

I suggest you start by making a single String from your String[] symbols. You might use a for-each loop and a StringBuilder and something like

    String[] symbols = { "a*", "j^", "y!" };
    StringBuilder sb = new StringBuilder();
    for (String symbol : symbols) {
        sb.append(symbol);
    }

Then you can use String.toCharArray() to convert that into a char[]

    char[] chars = sb.toString().toCharArray();
    System.out.println(Arrays.toString(chars));

Output is

[a, *, j, ^, y, !]
Sign up to request clarification or add additional context in comments.

Comments

1

I won't give you a full solution, but I'll guide you so you have something to begin with.

Iterate on symbols array, it contains String. You can construct a new char array using String#toCharArray, then you'll have each char from each String that you can easily create a String from it and insert it to the new array.

2 Comments

He changed the question... it's a char[] for the result, not a String[]
@Ascalonian This doesn't change anything... OP can simply not convert the char to String. I just want to guide him..
1

This might solve your problem:

int length = 0;
for(final String s : symbol) { //first get the length of the new array
    length += s.length;
}
char[] result = new char[length];
int pos = 0;
for(final String s : symbol) {  //then fill the new array with data
    System.arraycopy(s.toCharArray(), 0, result, pos, s.length());
    pos += s.length();
}

So first of all, it counts how long the result will be by just summing up the lengths of all inputs. This is necessary in order to initialize the char[] with a given length.

Now we already have our empty result array with the right size and can simply fill it with our input. We do this by iterating through every input string and copying its .getCharArray() into the result array at the current position. After each input symbol copied into our result, we increase pos so that the next entry will be copied into the right position.

JavaDoc of System.arraycopy(src, srcPos, dest, destPos, length)

This also uses no unnecessary Collection API classes and works fine with Java primitives :)

2 Comments

@ElliottFrisch Just edited it. I just declare most local fields as final because it prevents one from accidentally changing it. It's just a habit.
Two passes, but it is very nice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.