A very basic and simple approach to convert String into ArrayList.
if your String looks like -
String input[] = {"one","two","three"};
import java.util.ArrayList; import java.util.List; public class Javaoneworld { public static void main(String[] args) { String input[] = {"one","two","three"}; List<String> arrList = new ArrayList<>(); for(String s:input){ arrList.add(s); } System.out.print(arrList); } }
And if String looks like below -
String input = "[one,two,three]";
Then -
import java.util.ArrayList; import java.util.List; public class First { public static void main(String[] args) { List<String> arrList = new ArrayList<>(); String input = "[one,two,three]"; String stringArr[] = input .replace("[","") .replace("]","") .split(","); for(String s:stringArr){ arrList.add(s); } System.out.print(arrList); } }
And if String is -
String input = "[\"one\",\"two\",\"three\"]";
then -
import java.util.ArrayList; import java.util.List; public class First { public static void main(String[] args) { String input = "[\"one\",\"two\",\"three\"]"; Gson gson = new Gson(); JsonArray jsonArray = gson.fromJson(input, JsonArray.class); List<String> arrList = new ArrayList<>(); for (JsonElement jsonElement : jsonArray) { String s = jsonElement.getAsString(); arrList.add(s); } System.out.println(arrList); } }
No comments:
Post a Comment