Privacy Policy Terms Of Use. Copyright © 2006-2010 Java Tutorials and Examples.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
- import java.util.ArrayList;
- //Java String to ArrayList
- public class StringToArrayList {
- public static void main(String args[]){
- //original string containing numbers
- String strNumbers = "One Two Three Four Five";
- //split the string using split method of String class
- String[] numbers = strNumbers.split(" ");
- //create new ArrayList object
- ArrayList<String> aListNumbers = new ArrayList<String>();
- //add individual numbers to the ArrayLists
- for(int i=0; i< numbers.length; i++){
- aListNumbers.add(numbers[i]);
- }
- System.out.println("String converted to ArrayList: " + aListNumbers);
- }
- }
- /*
- Output of the above given Java String to ArrayList example would be
- String converted to ArrayList: [One, Two, Three, Four, Five]
- */

Post new comment