Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Appending ArrayLists... 1

Status
Not open for further replies.

Exie

Programmer
Sep 3, 2003
156
AU
Hi,

I have a loop, which iterates over an arraylist, and when certain conditions are met, it calls a method with some parameters, which returns a new arraylist.

As I may be calling this many time (for each iteration of my loop), I end up with many different arraylists. Is there any easy way of "glue"ing them all together. My goal is to end up with one list.
Code:
ArrayList al = new ArrayList();
try {
   LDAPUtility lu = new LDAPUtility();

   // List of my roles
   ErmUserBean eb = (ErmUserBean) request.getSession().getAttribute("userBean");
   ArrayList rolesList = eb.getExtranetRoles();
			
   for (Iterator iter = rolesList.iterator(); iter.hasNext();) {
      String role = (String) iter.next();
		
      // Is this role valid to access this report ?
      if(reportGrps.contains(role)){

         //TODO Concatenate Arraylists with each iteration.
         al.add(lu.getUserIDInGroups(role));
      }
   }
}

Note that the [blue]getUserIDInGroups()[/blue] will always return an arraylist. At present, the code above gives me an array of arrays so to speak.

I'm not sure if it's possible/practicle to expand the results from [blue]getUserIDInGroups()[/blue] before storing it.

Ultimately, my goal is to stuff my big single list in the request scope so I can render it in a JSP. Maybe I should be using a vector or a collection or something <shug>.

Any feedback would be appreciated.
 
The below example should give you an idea as to what you need.

It has a "master" ArrayList, and demonstrates how to add the contents of several other ArrayLists to the master :

Code:
public class Test {
 	public static void main(String args[]) {

		ArrayList master = new ArrayList();

		ArrayList al = new ArrayList();
		al.add("1");
		al.add("2");
		al.add("3");

		ArrayList al2 = new ArrayList();
		al2.add("a");
		al2.add("b");
		al2.add("c");

		master.addAll(al);
		master.addAll(al2);

		System.err.println(master);
           }
}

--------------------------------------------------
Free Database Connection Pooling Software
 
Hi,

Hey, thanks Sedj! The problem as it turns out was the add() method for arraylists.

When I look in my debugger, and I expand the "[red]master[/red]" object, I didnt have 6 strings.

I had 2 objects "[blue]al[/blue]" and "[blue]al2[/blue]", each with 3 strings.

What I was trying to do, is end up with 6 strings(or 'string' objects if you like) in the "[red]master[/red]" object.

By changing the add() to addAll(), I now have 6 strings. :)

Thanks folks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top