0

I have ArrayList. I need split each element of this ArrayList by "," and as result got ArrayList<ArrayList<String>>. How to do it properly? This is code I have tried:

ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
   try {
    ArrayList<String> lines = rf.readLines(filename);         
        for (int i = 0; i<=lines.size(); i++){
            String[] items = lines.get(i).split(",");
        }
    }  
    catch(IOException e)  
    {   
        System.out.println("Unable to create "+filename+": "+e.getMessage());                
    } 
4
  • so what is the problem or error ? Commented Feb 9, 2013 at 14:41
  • 1
    Now you only need to move the String[] into an ArrayList<String>. You can use the Arrays#asList method to continue solving your problem. Commented Feb 9, 2013 at 14:42
  • Your question is not clear - I lost your meaning at "...as as result got ArrayList>." You do not define what you mean by "do it properly". Expand, let us know if you're getting an error and what it is, be sure to define the output you want. Commented Feb 9, 2013 at 14:42
  • 1
    You should go only to i < lines.size(), not <= Commented Feb 9, 2013 at 14:43

1 Answer 1

4

You need to convert the line array into an ArrayList and add that to list. This should do the trick.

list.add(new ArrayList<String>(Arrays.asList(items)));
Sign up to request clarification or add additional context in comments.

1 Comment

Yep. Or he could also build an ArrayList<List<String>>, and directly store the result of Arrays.asList(items).

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.