Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help me get out of this loop problem

 
anitha nair
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anand Hariharan
Rancher
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mea Culpa. Criticism/advice well-received and taken.

sincerely,
- Anand
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic