Hi ,

I have problem related to ArrayList......

ArrayList object may contain 1 or more ArrayList Objects and those inner ArrayList Object may contain 1 or more ArrayList Object and it is extendable to n levels. Condition is also added as ArrayList may contain different type of Objects too, i.e. String, Integer, TreeSet etc.

Problem is to find total number of ArrayList Object.

Can anyone here will give me code snippest to solve this problems

Hi ,

I have problem related to ArrayList......

ArrayList object may contain 1 or more ArrayList Objects and those inner ArrayList Object may contain 1 or more ArrayList Object and it is extendable to n levels. Condition is also added as ArrayList may contain different type of Objects too, i.e. String, Integer, TreeSet etc.

Problem is to find total number of ArrayList Object.

Can anyone here will give me code snippest to solve this problems

Use recursion :).

can you provide some code to use recursion in that case?

can you provide some code to use recursion in that case?

@SuppressWarnings("unchecked")
    function recursiveGet(ArrayList list) {
	while (true) {
	    store list size in a variable
	    result=result + list size 
	    if  list size is different than 0 {
		for every element in the list {
		   create a new array list with every element
		    recursiveGet( new element );
		}
	    }
	    break;
	}
    }

nomemory: Problem statement asserts ArrayList may contain non-ArrayList objects.

nomemory: Problem statement asserts ArrayList may contain non-ArrayList objects.

Sorry, then he should check every object using "instanceof" operator. But the algorithm will be slightly different...

It's tricky to help you with this without just giving you a working code sample (which is against the rules here). My first attempt would be a little different than nomemory's. .

public int getListCount(ArrayList list){
    int count = 0;
    for (every object in list){
           if the object is an arraylist, count++;
           if the object is an arraylist, count+= getListCount(the object);
     }

     return count;
}

There might be a flaw in that strategy, but none that I can think of off the top of my head. If so, someone will be sure to tear it apart though. Either way, recursion can be difficult to grasp - I'd recommend that you look at some classic examples of recursion, such as how recursion can be used to count the nodes in a binary tree and other data structures type examples that are widely available on the net.

There might be a flaw in that strategy, but none that I can think of off the top of my head. If so, someone will be sure to tear it apart though.

No, that's exactly the right strategy. Just needs to be re-cast into formal Java code.

commented: hi please reply for my doubt +0

hi...i am new with this field......i created a bean like

public CustomBean(){

        customObjectList = new ArrayList(5);
        selectBrand=new ArrayList();

}
        customObjectList.add(new CustomObject("Acer phones"
                                              ));
        customObjectList.add(new CustomObject(" Acer phones"
 ));
        customObjectList.add(new CustomObject("Amoi phones"
                                              ));
       // }
       // else if(this.phone==1)
       // {
        customObjectList.add(new CustomObject("Apple phones"
                                              ));
        customObjectList.add(new CustomObject("Asus phones"
                                              ));
        customObjectList.add(new CustomObject("ATT phones"
                                              ));
        customObjectList.add(new CustomObject("Benefon phones"
                                              ));
        customObjectList.add(new CustomObject("BenQ phones"
                                              ));
        customObjectList.add(new CustomObject("BenQ-Siemens phones"
                                              ));

i need to give count for each of these Strings.....like acer,apple etc....can anyone help me

each of these customObjectLists were linked to commandlinks option in icefaces........

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.