I have many Arraylists having String objects , and I have a requirement to concatenate there values.
Eg:
ArrayList finalList = new ArrayList();
ArrayList catMe = new ArrayList();
ArrayList x = new ArrayList();
x.add("Green");
x.add("Red");
ArrayList y = new ArrayList();
y.add(" Apple");
//......
catMe.add(x);
catMe.add(y);
concatContents(catMe); // Here i need to do
// some concatenation magic.
so when finalList is printed:
finalList.get(0) // should show > "Green Apple"
finalList.get(1) // should show > "Red Apple"
I know it looks easy if there are only two list X and Y… but I need it for n dimensions. Say if there is 3rd list
ArrayList z= new ArrayList();
z.add(" USA");
z.add(" Canada");
catMe.add(z);
concatContents(catMe);
Now finalList should show
Green Apple USA
Green Apple Canada
Red Apple USA
Red Apple Canada
Do i need recursion? Unable to think how to implement though! Do any java master there have a solution?
Here’s a recursive answer. Just cooked it up, so no guarantees on quality… 🙂