0

My arraylist consists of an array with a list of strings, each string inside is separated by commas, i want to split one of the strings into substrings divided by the commas, i know how to do it to arrays by using the split method, but im having trouble finding something similar to array lists, heres the code:

String[] widgets2 = 
        {

            "1,John,Smith,[email protected],20,88,79,59",
            "2,Suzan,Erickson,Erickson_1990@gmailcom,19,91,72,85",
            "3,Jack,Napoli,The_lawyer99yahoo.com,19,85,84,87",
            "4,Erin,Black,[email protected],22,91,98,82",
            "5,Adan,Ramirez,[email protected],100,100,100"

        };

         ArrayList<String> jim = new ArrayList<String>(Arrays.asList(widgets2));

System.out.println(jim.split(0));
10
  • Java 7: loops, loops and more loops (well, just one explicit loop: pretend it is C). Commented Aug 31, 2015 at 4:58
  • jim.get(index).split() probably? How did you do it in arrays? Commented Aug 31, 2015 at 4:58
  • 2
    Please change your profile picture. Commented Aug 31, 2015 at 4:58
  • jim.get(0).split(",") may be? Commented Aug 31, 2015 at 5:01
  • jim.get(0).split(",") produces: [Ljava.lang.String;@19e0bfd Commented Aug 31, 2015 at 5:06

3 Answers 3

1

It is similar to what you do with the arrays. Just that the implementation is different. To get the values just loop through them:

List<String> jim = new ArrayList<String>(Arrays.asList(widgets2));
for(String currentString : jim){//ArrayList looping
 String[] separatedStrings = currentString.split(",");
    for(String separatedString : separatedStrings){//Now its array looping
       //Do something now whatever you like to
    }
}

If you want to have the index and decide which value to get, use normal for loop using index and use jim.get(index) and use split().

Having said that, what stops you from looping through the List ? It's the common usage of array list - to loop through it :)

Sign up to request clarification or add additional context in comments.

Comments

0
    String jim_string = jim.toString().replace("[", "");
    jim_string.replace("]", "");
    String splitted[] = jim_string.split(",");
    System.out.println(splitted[0]);

Comments

0

put the below code lines into your code, it'll give your desire output.

int indx=0; //use the index you want to get & split
String tmp = jim.get(indx); // get the string from the ArrayList
if(tmp!=null && !tmp.equals("")){ // null check
for (String retval: tmp.split(";")){ // split & print
     System.out.println(retval);
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.