aspose file tools*
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
Author

help me get out of this loop problem

anitha nair
Greenhorn

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
Rancher

Joined: Aug 22, 2006
Posts: 257

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.

HTH,
- 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
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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
Rancher

Joined: Aug 22, 2006
Posts: 257

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

sincerely,
- 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)
{
details=k;
if(hashMap.containsKey(details.source())
{
int indi=hashMap.get(details.source());
similarObjList.get(indi).add(details);
}
else
{
similarObjList.add(indicator,new ArrayList());
hashMap.put(details.source(),""+indicator);
indicator++;
}

}
i++;
}

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

//Now you can Print it. in XML format

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

ArrayList tempList=similarObjList.get(i);

Print What ever format you want .....

i++;
}
print(<XML CLASS="END"> ;


------

New changes are welcome

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: help me get out of this loop problem