Reputation: 81
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?
Upvotes: 0
Views: 1241
Reputation: 35577
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]
Upvotes: 4
Reputation: 9795
int charCode = (int) c.charValue();
In your example
List<Integer> result = new ArrayList<Integer>();
for(Character c : a) {
result.add((int) c.charValue());
}
Upvotes: 1
Reputation: 13346
You simply have to convert it into int
.
ArrayList<Integer> s = new ArrayList<Integer>();
for(Character c: a) {
s.add((int) c)
}
Upvotes: 1