Java(ArrayList of Character) Write a method that returns an array list of Character from a string using the following header: public static ArrayList<Character> toCharacterArray(String s) For example, toCharacterArray("abc") returns an array list that contains characters 'a', 'b', and 'c'.
时间: 2023-11-22 07:54:58 浏览: 69
C#中数组Array,ArrayList,泛型List详细对比
Sure, here's an implementation of the toCharacterArray method in Java:
```
import java.util.ArrayList;
public class CharacterArrayConverter {
public static ArrayList<Character> toCharacterArray(String s) {
ArrayList<Character> charList = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
charList.add(s.charAt(i));
}
return charList;
}
}
```
This implementation creates an empty ArrayList<Character>, iterates through each character in the input string, and adds it to the ArrayList. Finally, it returns the ArrayList containing all the characters from the input string.
You can call this method by creating an instance of the CharacterArrayConverter class and calling the toCharacterArray method, like this:
```
CharacterArrayConverter converter = new CharacterArrayConverter();
ArrayList<Character> charList = converter.toCharacterArray("abc");
```
This will create an ArrayList containing the characters 'a', 'b', and 'c'.
阅读全文