Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ArrayList 1

Status
Not open for further replies.

sensory21

Programmer
Jan 27, 2004
79
0
0
GB
Hi,

how can I embed the sorted list iList I get at the end of the second while in the html I get in the first while?

Code:
ArrayList list = new ArrayList();
Iterator i = items.iterator();
while(i.hasNext())
{
double pc;
		
Item item = (Item) i.next();
if(item.getNextStage().getStageId() == Stage.DONE)
{
pc = 100.0;
} else {
int done = item.getHistory().size();
int total = item.getItemType().getStages().size();
pc = 100.0 * done / total;
}
list.add(new Double(pc));
		
String html = "";
html += "<td class=columnwhite>" +item.getItemType().getDescription()+  "</td>";
html += "<td class=columnwhite>" +item.getSystem().getName()+  "</td>";
html += "<td class=columnwhite>" +item.getJtd()+  "</td>";
html += "<td class=columnwhite>" +item.getTag()+  "</td>";
html += "<td class=columnwhite>" +item.getStage().getStageTemplate().getDescription()+  "</td>";
html += "<td class=columnwhite>" +item.getNextStage().getStageTemplate().getDescription()+  "</td>";

          //here I need the result of the sorted list
html += "<td class=columnwhite>" +roundPercentage.format(pc)+  "%</td>";
html += "<td class=columnwhite width=75><table width=100% border=0 cellspacing=0 cellpadding=0 style=border:1px solid #000000;><tr>";
if (pc > 0.00001) {
html += "<td width=" +pc+ "><img src=images/bargrph_blue.gif width=100% height=10></td>";
}
if (pc < 100.00000) {
html += "<td bgcolor=#CCCCFF><img src=images/bargrph_spacer.gif width=1 height=10></td>";
}
html += "</tr></table></td></tr>";

}//end while

Collections.sort(list);
Iterator iList = list.iterator();
while(iList.hasNext())
{
out.print(iList.next());
}
It was ok previously with SortedMap except this class doesn't allow duplicates, and I need to print out duplicates too which ArrayList allows?

Thanks for your help.
 
You need to divide your code into methods for clarity and ease of maintenance.
You should be using StringBuffer for this kind of work.
Here is an example:
Code:
private void buildCell(final StringBuffer target, final String value){
      target.append("<td class=columnwhite>");
      target.append(value);
      target.append("</td>");
}
Once all your html snippets are organized this way, you might have a method that takes List or Iterator, and builds the whole report(?). The calling code could create the List.
 
Thanks for your advice, I have my code working the way I want now with SortedMap, just sticked an ID at the end of the elements needed and got all the elements as unique.

So it's fine now with SortedMap and I've got the list in ascending order, do you know about the reversed() method to get the descending order?
 
Thanks for the StringBuffer class that's a lot better!
 
You're welcome. The way I understand the javadoc, is that SortedMap imposes the sort order that you choose when you create it (or natural, if you don't specify any).
You can provide a Comparator. If you needed to create a
map that sorted in the opposite order, you could take the
original map and extract the reverse Comparator to build a new map. If you filled it with elements from the old map, elements would be sorted in the opposite order:
Code:
SortedMap map1 = new TreeMap();
//fill it...
SortedMap map2 = new TreeMap(Collections.reverseOrder());
//iterate map1 and insert items into map 2.
Unfortunately, there is not a constructor that takes both
a map and a comparator.
 
Ah, I see you cross-posted this question - I hope that I don't get mod'd for ripping off someone's answer, mine is straight from javadocs ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top