Solved

Convert multi-layered arrayList to single-layered

Posted on 2001-06-16
2
219 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
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 96
null output 3 24
Java string replace 11 43
Is Applet the way to go for my drag and drop system? 8 10
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 …
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

785 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