0
ArrayList<Character> a = new ArrayList<Character>();
a.add('A');
a.add('B');
a.add('C');
ArrayList<Integer> s = (ArrayList<Integer>) a.clone();
System.out.println("Character: " + a + "\n Integer: " + s);

I need the answer as:

Character: [A, B, C]
Integer: [65, 66, 67]

How can I achieve this?

Tom
  • 16,842
  • 17
  • 45
  • 54
Ganesh NB
  • 81
  • 1
  • 14

3 Answers3

4

You can try this way.

List<Character> a=new ArrayList<Character>();
a.add('A');
a.add('B');
a.add('C');

List<Integer> s= new ArrayList<Integer>();
for(Character i:a){
   s.add(Integer.valueOf(i));
}

System.out.println("Character: "+a +"\nInteger: "+s);

Out put:

Character: [A, B, C]
Integer: [65, 66, 67]
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
1

You simply have to convert it into int.

ArrayList<Integer> s = new ArrayList<Integer>();

for(Character c: a) {
    s.add((int) c)
}
Till Rohrmann
  • 13,148
  • 1
  • 25
  • 51
1
int charCode = (int) c.charValue();

In your example

List<Integer> result = new ArrayList<Integer>();

for(Character c : a) {
    result.add((int) c.charValue());
}
darijan
  • 9,725
  • 25
  • 38