0

I have one arraylist that contain two list like this

[[asd, asswwde, efef rgg], [asd2223, asswwd2323e, efef343 rgg]]

My Code is

ArrayList<String> create = new ArrayList<String>();
ArrayList<String> inner = new ArrayList<String>();
ArrayList<String> inner1 = new ArrayList<String>();
inner.add("asd");
inner.add("asswwde");
inner.add("efef rgg");

inner1.add("asd2223");
inner1.add("asswwd2323e");
inner1.add("efef343 rgg");

create.add(inner.toString());
create.add(inner1.toString());

i have to get all value one by one of every index of that arraylist

So what is the best way to get these all value one by one. I am using JAVA with Eclipse Mars.

1
  • To avoid people getting frustrated - you should rephrase your question. Your question is really not about the nested ArrayList, but instead how to parse the java String "[asd2223, asswwd2323e, efef343 rgg]" back into individual elements (also String). Commented Apr 29, 2016 at 23:03

4 Answers 4

3

Just use two nested loops:

List<List<Object>> list = ...;
for (List<Object> subList : list) {
  for (Object o : subList) {
    //work with o here
  }
}

You may also want to consider replacing the inner lists by proper objects.

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

5 Comments

@user3441151 Why do you convert the list to a string? That will make your life a lot more difficult when you want to extract the values...
because of requirement, please update your code as per my requirement.
What requires you to make it a string? It makes no sense.
@user3441151 so your real question is how to parse the string back into it's individual elements for looping?
@YoYo yes individual elements for looping.
1

You want to loop through the outside ArrayList and then loop through each ArrayList within this ArrayList, you can do this by using the following:

for (int i = 0; i < outerArrayList.size(); i++)
{
    for (int j = 0; j < outerArrayList.get(i).size(); j++)
    {
        String element = outerArrayList.get(i).get(j);
    }
}

Here is another verison you may find easier to understand, but is essentially the same:

for (int i = 0; i < outerArrayList.size(); i++)
{
    ArrayList<String>() innerArrayList = outerArrayList.get(i)

    for (int j = 0; j < innerArrayList.size(); j++)
    {
        String element = innerArrayList.get(j);
    }
}

or alternatively again using a foreach loop:

for (ArrayList<String> innerArrayList : outerArrayList)
{
    for (String element : innerArrayList)
    {
        String theElement = element;
    }
}

It might be worth noting that your ArrayList appears to contain different types of elements - is this definitely what you wanted to do? Also, make sure you surround your strings with "" unless they are variable names - which it doesn't appear so.

EDIT: Updated elements to type String as per your update.

I would also recommend you change the type of your create ArrayList, like below, as you know it will be storing multiple elements of type ArrayList:

ArrayList<ArrayList> create = new ArrayList<ArrayList>();

4 Comments

The first two examples will skip the last element.
@PietervandenHam correct, thanks for pointing that out, have a habit of sticking -1 everywhere :p
@James i followed your 2nd answer this gives me error.
@user3441151 my answer is syntacically correct, you need to adapt it for your exact needs - we are here to help, not offer a coding service.
1

Try to use for loop nested in foreach loop like this:

 for(List list : arrayListOfList)
  {
   for(int i= 0; i < list.size();i++){
      System.out.println(list.get(i));
    }
  }

Comments

0

I'm not sure if the data structures are part of the requirements, but it would be better constructed if your outer ArrayList used ArrayList as the generic type.

ArrayList<ArrayList<String>> create = new ArrayList<ArrayList<String>>();
ArrayList<String> inner = new ArrayList<String>();
ArrayList<String> inner1 = new ArrayList<String>();

...

create.add(inner);
create.add(inner1);

Then you could print them out like this:

for(List list : create) {
    for (String val : list) {
       System.out.println(val);
    }
}

Othewise, if you stick with your original code, when you add to the outer list you are using the toString() method on an ArrayList. This will produce a comma delimited string of values surrounded by brackets (ex. [val1, val2]). If you want to actually print out the individual values without the brackets, etc, you will have to convert the string back to an array (or list) doing something like this:

for (String valList : create) {
    String[] vals = valList.substring(1, val.length() - 1).split(",");
    for (String val : vals) {
        System.out.println(val.trim());
    }
}

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.