Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Char Array to List in Java

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.

like image 462
bourne Avatar asked Mar 23 '13 18:03

bourne


People also ask

Can we convert array to ArrayList in Java?

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.

What method is used to convert from an array to a List in Java?

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);

Can we convert array to List?

We can use Arrays. asList() method to convert a Java array to List easily.


2 Answers

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.

like image 118
hotkey Avatar answered Oct 02 '22 13:10

hotkey


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]); :)

like image 35
dantuch Avatar answered Oct 02 '22 13:10

dantuch