File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes help me get out of this loop problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "help me get out of this loop problem" Watch "help me get out of this loop problem" New topic

help me get out of this loop problem

anitha nair

Joined: Jul 12, 2006
Posts: 12
hai forum,

I am have trouble to fix this up.Please help me out with your valuable suggestion.

I have an arrayList having many objects.I am saving the details of these objects in Xml format.I am trying to place the objects of same class inside a tag<class id="--"> details </class>
My code prints the similar object details twice.I could understand that its because the checking is being done in both loops.But when i do it using a single for loop i have difficulty placing the tags properly.I am providing a rough format of my code.Please suggest me how can i accomplish this idea efficiently.

Thank you all.
Anand Hariharan

Joined: Aug 22, 2006
Posts: 272

Sort your "arraylist" based on your key ("details.source"). This has a O(n lg n) complexity as opposed to the O(n ^ 2) complexity that your implementation currently has.

Now print out your arraylist. You should have what you want.

- Anand

"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Hi Anand --

Welcome to JavaRanch!

I notice that your signature file is rather long compared to what we usually see here at the ranch -- it's longer that your post in this thread, and longer than many posts in many threads, come to think of it. The fact that it's mostly code makes it especially distracting and makes threads in which it appears hard to read.

The worst part is that people rapidly tire of seeing the same joke over and over again.

Please, as soon as you can, head here and clean yours up, please; a single line would be most appropriate. Thanks.

[Jess in Action][AskingGoodQuestions]
Anand Hariharan

Joined: Aug 22, 2006
Posts: 272

Mea Culpa. Criticism/advice well-received and taken.

- Anand
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257

Anitha, See this works for you, and I have used this in my applicatin.


Initialize a HashMap <hashMap>.
Create a ArrayList similarObjList;
int i=0;
int indicator=0;
Details details;
while i< ArrayList.size()
Object k=ArrayList.get(i);

if(k instanceof Details)
int indi=hashMap.get(details.source());
similarObjList.add(indicator,new ArrayList());


// By this time the similar objects are grouped. See it o(n).

//Now you can Print it. in XML format

print(<XML CLASS="IDIN"> ;
whle(i< similarObjList.size())

ArrayList tempList=similarObjList.get(i);

Print What ever format you want .....

print(<XML CLASS="END"> ;


New changes are welcome

I agree. Here's the link:
subject: help me get out of this loop problem
It's not a secret anymore!