Top community members
All Wiki Articles Create Wiki Article

In the past we had a dream,
to have place where we could share IT knowledge,
to ask questions without fear that someone will judge us.

Now we are a group of people who make this dream come true. ❤ 💻

If you think sharing knowledge and helping other is valuable.

join our community - Click here

Java - convert character array to integer array

0 contributions
6 points

In this quick post, we are going to check how to convert character array to integer array in java.

1. Convert char[] to int[]

import java.util.Arrays;

public class Example1 {

    public static void main(String[] args) {
        char[] charArray = {'a', 'b', 'c'};
        int[] intArray = asIntArray(charArray);

        // [97, 98, 99]
        System.out.println(Arrays.toString(intArray));
    }

    public static int[] asIntArray(char[] charArray) {
        int[] result = new int[charArray.length];
        Arrays.setAll(result, i -> (int) charArray[i]);
        return result;
    }
}

Output:

[97, 98, 99]

2. Convert char[] to Integer[]

import java.util.Arrays;

public class Example2 {

    public static void main(String[] args) {
        char[] charArray = {'a', 'b', 'c'};
        Integer[] integerArray = asIntegerArray(charArray);

        // [97, 98, 99]
        System.out.println(Arrays.toString(integerArray));
    }

    public static Integer[] asIntegerArray(char[] charArray) {
        Integer[] result = new Integer[charArray.length];
        Arrays.setAll(result, i -> (int) charArray[i]);
        return result;
    }
}

Output:

[97, 98, 99]

3. Convert Character[] to int[]

import java.util.Arrays;

public class Example3 {

    public static void main(String[] args) {
        Character[] characterArray = {'a', 'b', 'c'};
        int[] intArray = asIntArray(characterArray);

        // [97, 98, 99]
        System.out.println(Arrays.toString(intArray));
    }

    public static int[] asIntArray(Character[] charArray) {
        return Arrays.stream(charArray).mapToInt(i -> (int) i).toArray();
    }
}

Output:

[97, 98, 99]

4. Convert Character[] to Integer[]

import java.util.Arrays;

public class Example4 {

    public static void main(String[] args) {
        Character[] characterArray = {'a', 'b', 'c'};
        Integer[] integerArray = asIntegerArray(characterArray);

        // [97, 98, 99]
        System.out.println(Arrays.toString(integerArray));
    }

    public static Integer[] asIntegerArray(Character[] charArray) {
        return Arrays.stream(charArray)
                .mapToInt(Integer::valueOf)
                .boxed()
                .toArray(Integer[]::new);
    }
}

Output:

[97, 98, 99]

 

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more