Can anyone help me and tell how to convert a char
array to a list and vice versa. I am trying to write a program in which users enters a string (e.g "Mike is good"
) and in the output, each whitespace is replaced by "%20"
(I.e "Mike%20is%20good"
). Although this can be done in many ways but since insertion and deletion take O(1) time in linked list I thought of trying it with a linked list. I am looking for someway of converting a char
array to a list, updating the list and then converting it back.
public class apples { public static void main(String args[]) { Scanner input = new Scanner(System.in); StringBuffer sb = new StringBuffer(input.nextLine()); String s = sb.toString(); char[] c = s.toCharArray(); //LinkedList<char> l = new LinkedList<char>(Arrays.asList(c)); /* giving error "Syntax error on token " char", Dimensions expected after this token"*/ } }
So in this program the user is entering the string, which I am storing in a StringBuffer
, which I am first converting to a string and then to a char
array, but I am not able to get a list l
from s
.
I would be very grateful if someone can please tell the correct way to convert char
array to a list and also vice versa.
We can convert an array to arraylist using following ways. Using Arrays. asList() method - Pass the required array to this method and get a List object and pass it as a parameter to the constructor of the ArrayList class.
Convert your array to a List with the Arrays. asList utility method. Integer[] numbers = new Integer[] { 1, 2, 3 }; List<Integer> list = Arrays. asList(numbers);
We can use Arrays. asList() method to convert a Java array to List easily.
In Java 8 and above, you can use the String
's method chars()
:
myString.chars().mapToObj(c -> (char) c).collect(Collectors.toList());
And if you need to convert char[]
to List<Character>
, you might create a String
from it first and then apply the above solution. Though it won't be very readable and pretty, it will be quite short.
Because char is primitive type, standard Arrays.asList(char[])
won't work. It will produce List<char[]>
in place of List<Character>
... so what's left is to iterate over array, and fill new list with the data from that array:
public static void main(String[] args) { String s = "asdasdasda"; char[] chars = s.toCharArray(); // List<Character> list = Arrays.asList(chars); // this does not compile, List<char[]> asList = Arrays.asList(chars); // because this DOES compile. List<Character> listC = new ArrayList<Character>(); for (char c : chars) { listC.add(c); } }
And this is how you convert List back to array:
Character[] array = listC.toArray(new Character[listC.size()]);
Funny thing is why List<char[]> asList = Arrays.asList(chars);
does what it does: asList
can take array or vararg. In this case char [] chars
is considered as single valued vararg of char[]
! So you can also write something like
List<char[]> asList = Arrays.asList(chars, new char[1]);
:)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With