6

Is it possible to cast an int array to a char array? If so - how?


I'm currently working on a project where I need to create an char array containing the alphabet. My current code creats an int array (which should be converted to an char array - in one Line!):

return IntStream.range('a', 'z' + 1).toArray();

3
  • If you want to convert a digit (0-9), you can add 48 to it and cast, or something like Character.forDigit(a, 10);. Commented Oct 7, 2016 at 7:37
  • yes it is possible , just convert your number into 16 based number , consists of a-f and 0-9 :)) Commented Oct 7, 2016 at 7:52
  • 3
    No, you can't cast int[] to char[]. Also, your current code doesn't work, so it doesn't matter how short it is. First rule of optimization: It has to work. Since Java 8 doesn't have a CharStream, you should do it with a normal for loop. It only takes 3 lines of code (4 with return statement), and it works! Commented Oct 7, 2016 at 7:55

4 Answers 4

14

Yeah, we’re missing a stream method to produce a char array. Maybe a whole CharStream class. In any case, no, you cannot cast between int[] and char[].

In the meantime, it’s getting a long line, but it works:

    return IntStream.rangeClosed('a', 'z')
            .mapToObj(c -> Character.toString((char) c))
            .collect(Collectors.joining())
            .toCharArray();

This gives a char[] containing

[a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]
Sign up to request clarification or add additional context in comments.

4 Comments

Works like a charm! +1 becuase you arren't a "This wont work"- person :P
IntStream.range('a', 'z' + 1) can be replaced with IntStream.rangeClosed('a', 'z')
@Pshemo, thx, it may belong as a comment to the question (from where I took IntStream.range('a', 'z' + 1)), but in any case, it’s clearly more readable.
A downside to this solution is that a String object is created for each array element — this could be a performance / memory consideration for larger arrays.
2

From Java-11 and onwards , you can use .mapToObj(Character::toString) instead of .mapToObj(c -> Character.toString((char) c)) , so your overall code boils down to :

return IntStream.rangeClosed('a', 'z')
        .mapToObj(Character::toString)
        .collect(Collectors.joining())
        .toCharArray();

Comments

1

Let's keep it one line then:

return IntStream.range('a', 'z' + 1).mapToObj(i -> Character.valueOf((char) i)).toArray(Character[]::new);

This converts from IntStream, to Stream<Character>. Keep in mind chars and ints are essentially the same in terms of many calculations, so this step may be unnecessary (especially for comparisons).

Edit:

Fixed the above line to be functional, there is a better solution but I'm still trying to find it again. It currently returns a Character[] array.

Without the 1 line restriction it's simple to just remake the array, treating a as your 0 index.

char[] back = new char[('z' + 1) - 'a'];
IntStream.range('a', 'z' + 1).forEach(i -> back[i - 'a'] = (char) i);
return back;

3 Comments

that's what I'm talking about! The only problem is, that it returns an object array - how can I cast it to an char array? :3
i am not sure if its just my compiler but your solution leads to following error for me: Error: java: incompatible types: java.lang.Integer cannot be converted to char
No you're both correct, there's a better way I'll edit it in in just a moment.
0

You cannot cast int array to char array because there are absolutely different types.

There are 2 possible options:

1) create a char array and copy each value from int array 2) use generic lists or collections. You can simple use it for casting

3 Comments

@TheRealVira streams does it. not arrays
Just to keep the usage correct, @AlexKlimashevsky is correct, my answer is not doing a cast, but solving your problem by other means.
@AlexKlimashevsky plus there is a way to get a stream from your array: stackoverflow.com/questions/27888429/…

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.