Reputation: 1058
String x = "abcd";
Arrays.asList(x.toCharArray()).forEach(y -> System.out.println(y + 1));
// this line is giving an error that + operator is undefined for char[] and int.
Arrays.asList(new Integer[]{1, 2}).forEach(y -> {System.out.println(y + 1);});
// for integer it is working fine.
When we use +
operator for doing something like this 'a' + 1
we get 98 because ASCII value of a is 97.
So why it is not working with char array in the above case.
Any thoughts on this?
Upvotes: 1
Views: 356
Reputation: 60016
Arrays.asList
consider x.toCharArray()
as an object because x.toCharArray()
return char[]
, so it is considers as one element, and you can't make char[] + int
, to solve this you can use x.chars()
:
x.chars().forEach(y -> System.out.println(y + 1));
Upvotes: 3