Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Convert multi-layered arrayList to single-layered

Posted on 2001-06-16
2
Medium Priority
?
228 Views
Last Modified: 2010-03-31
I have an arrayList which contains items and other arrayLists which also contain items, ie multi-layered. How can I return an arrayList which is single layered, ie contains only the items in each arrayList and contains no arrayLists?
0
Comment
Question by:ncw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Accepted Solution

by:
superschlonz earned 300 total points
ID: 6199027
public ArrayList createSingleLayered( ArrayList multiList )
{
  ArrayList singleList = new ArrayList();
  createSingleLayered( multiList, singleList );
  return singleList;
}
private void createSingleLayered( ArrayList multiList, ArrayList singleList )
{
  for( int i=0; i<multiList.size(); ++i )
  {
    Object obj = multiList.get( i );
    if( obj instanceof ArrayList )
    {
      createSingleLayered( (ArrayList)obj, singleList );
    }
    else
    {
      singleList.add( obj );
    }
  }
}
0
 
LVL 1

Author Comment

by:ncw
ID: 6199585
Excellent, thanks
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

598 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question