?
Solved

Convert multi-layered arrayList to single-layered

Posted on 2001-06-16
2
Medium Priority
?
225 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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
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 …
Suggested Courses
Course of the Month8 days, 7 hours left to enroll

766 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