+ 1
Problem converting Array to ArrayList
Same error with E, T example https://code.sololearn.com/cmNQH014sVF0/?ref=app
3 ответов
+ 3
You could use these for arrays
char[] arr = {'2', '5'};
ArrayList<Character> list = new ArrayList<Character>();
for (char ch: arr) list.add(ch);
or
Character[] arr = {'2', '5'};
ArrayList<Character> list = new ArrayList<>(Arrays.asList(arr));
or
Character[] arr = {'2', '5'};
ArrayList<Character> list = new ArrayList<>();
list.addAll(Arrays.asList(arr));
This is the result.
for (char i : list) System.out.println(i);
or
for (Character i : list) System.out.println(i);
// Keep learning & happy coding :D
+ 2
I think asList doesn't work for char arrays. Maybe try
ArrayList<Character> li = new ArrayList<Character>();
for (char ch: arr) {
li.add(ch);
}
0
Thank you both, now it works!