Chris Marker
Chris Marker

Reputation: 309

Converting ArrayList of Char into another ArrayList of Int

This has been bugging me for the last hour, and I feel I'm getting nowhere.

I have an ArrayList of characters, and I need to convert the number characters into integers, and put it in a new array. So:

ArrayList<Character> charList contains [5, ,1,5, ,7, ,1,1]

I'd like to take the current charList and put the contents into a new ArrayList of type Integer, which obviously wouldn't contain the spaces.

ArrayList<Integer> intList contains [5, 15, 7, 11]

Any help right now would be greatly appreciated.

Upvotes: 0

Views: 3184

Answers (8)

jota3
jota3

Reputation: 5756

With Java8 you can convert it that way :

String str = charList.stream().map(Object::toString).collect(Collectors.joining());
ArrayList<Integer> intList = Arrays.asList(str.split(" ")).stream().map(Integer::parseInt)
                                   .collect(Collectors.toCollection(ArrayList::new));

First we gather the characters separated by ',' into a string, then we split it into a list of its numbers (as Strings) and from a stream of that filters we parse every String into an int and we gather them into a new list.

Upvotes: 0

twain249
twain249

Reputation: 5706

Do you want to change each individual character into it's corresponding integer?

If so there is a

Integer.parseInt(String)

that converts a string to an int and a

Character.toString(char)

that converts the character to a string

If you want to convert multiple characters into a single integer you can convert all of the characters individually and then do something like this

int tens = Integer.parseInt(Character.toString(charList.get(i)));
int ones = Integer.parseInt((Character.toString(charList.get(i+1)));
int value = tens * 10 + ones;
intList.add(i, value);

Upvotes: 0

Judgemaik
Judgemaik

Reputation: 33

here another one ....without parseInt

        char[] charList = "5 15 7 11 1234 34 55".Trim().ToCharArray();
        List<int> intList = new List<int>();
        int n = 0;
        for(int i=0; i<charList.Length; i++)
        {
            if (charList[i] == ' ')
            {
                intList.Add(n);
                n = 0;
            }
            else
            {
                n = n * 10;
                int k = (int)(charList[i] - '0');
                n += k;
            }
        }

Upvotes: 0

Juvanis
Juvanis

Reputation: 25950

Iterate over the character list, parse them into integers and add to the integer list. You should be careful about NumberFormatException. Here is a fully working code for you:

Character[] chars = new Character[]{'5',' ','1','5',' ','7',' ','1','1'};
List<Character> charList = Arrays.asList(chars);
ArrayList<Integer> intList = new ArrayList<Integer>();

for(Character ch : charList)
{
    try
    { intList.add(Integer.parseInt(ch + "")); }
    catch(NumberFormatException e){}
}

If you have already populated your character list, you can skip the first 2 lines of the code above.

Upvotes: 0

maerics
maerics

Reputation: 156534

public static List<Integer> getIntList(List<Character> cs) {
  StringBuilder buf = new StringBuilder();
  List<Integer> is = new ArrayList<Integer>();
  for (Character c : cs) {
    if (c.equals(' ')) {
      is.add(Integer.parseInt(buf.toString()));
      buf = new StringBuilder();
    } else {
      buf.append(String.valueOf(c));
    }
  }
  is.add(Integer.parseInt(buf.toString()));
  return is;
}


List<Character> cs = Arrays.asList('5', ' ', '1', '5', ' ', '7', ' ', '1', '1');
List<Integer> is = getIntList(cs); // => [5, 15, 7, 11]

Upvotes: 0

Andreas Dolk
Andreas Dolk

Reputation: 114797

Walk through the char array, if it you see

  • a number: append it to the String(builder)
  • a space: parse the current String(builder)

Here's a trivial implementation:

StringBuilder sb = new StringBuilder();
List<Character> charList = getData();  // get Values from somewhere
List<Integer> intList = new ArrayList<Integer>();

for (char c:charList) {
  if (c != ' ') {
     sb.append(c);
  } else {
     intList.add(Integer.parseInt(sb.toString());
     sb = new StringBuilder();
  }
}

Upvotes: 0

vladimir e.
vladimir e.

Reputation: 723

for(Character ch : charList) {
    int x = Character.digit(ch, 10);
    if (x != -1) {
         intList.add(x);
    }
 }

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726809

First, make a String out of the characters in your charList, then split that string at the space, and finally parse each token into an int, like this:

char[] chars = new char[charList.size()];
charList.toArray(chars);
String s = new String(chars);
String[] tok = s.split(" ");
ArrayList<Integer> res = new ArrayList<Integer>();
for (String t : tok) {
    res.add(Integer.parseInt(t));
}

Upvotes: 3

Related Questions