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.
Because char is primitive type, standard
Arrays.asList(char[])
won't work. It will produceList<char[]>
in place ofList<Character>
... so what's left is to iterate over array, and fill new list with the data from that array:And this is how you convert List back to array:
Funny thing is why
List<char[]> asList = Arrays.asList(chars);
does what it does:asList
can take array or vararg. In this casechar [] chars
is considered as single valued vararg ofchar[]
! So you can also write something likeList<char[]> asList = Arrays.asList(chars, new char[1]);
:)I guess the simplest way to do this would be by simply iterating over the char array and adding each element to the ArrayList of Characters, in the following manner:
In Java 8 and above, you can use the
String
's methodchars()
:And if you need to convert
char[]
toList<Character>
, you might create aString
from it first and then apply the above solution. Though it won't be very readable and pretty, it will be quite short.All Operations can be done in java 8 or above:
To the Character array from a Given String
To get the Character List from given String
To get the characters set from given String Note: sets only stores unique value. so if you want to get only unique characters from a string, this can be used.
To get Characters in a specific range from given String : To get the character whose unicode value is greater than 118. https://unicode-table.com/en/#basic-latin
ASCII Code value for characters * a-z - 97 - 122 * A-Z - 65 - 90
It will return the characters: w,x, v, z
you ascii values in the filter you can play with characters. you can do operations on character in filter and then you can collect them in list or set as per you need
Another way than using a loop would be to use Guava's Chars.asList() method. Then the code to convert a String to a LinkedList of Character is just:
or, in a more Guava way:
The Guava library contains a lot of good stuff, so it's worth including it in your project.
You cannot use generics in java with primitive types, why?
If you really want to convert to List and back to array then dantuch's approach is the correct one.
But if you just want to do the replacement there are methods out there (namely
java.lang.String
's replaceAll) that can do it for youYou can use it like this:
Output: