0

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.

2
  • Have you looked at Guava's Collections2.transform() Commented Mar 5, 2012 at 15:11
  • @AmirPashazadeh there is no need to use Guava. Commented Mar 5, 2012 at 15:14

8 Answers 8

3

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));
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

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

Comments

0
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]

Comments

0

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.

Comments

0

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;
            }
        }

Comments

0

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

Comments

0

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.

Comments

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.