Solved

Convert multi-layered arrayList to single-layered

Posted on 2001-06-16
2
217 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
2 Comments
 
LVL 3

Accepted Solution

by:
superschlonz earned 75 total points
Comment Utility
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
Comment Utility
Excellent, thanks
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now