﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-borball-随笔分类-java基础</title><link>http://www.blogjava.net/borball/category/8152.html</link><description>borball的足迹</description><language>zh-cn</language><lastBuildDate>Fri, 02 Mar 2007 04:48:31 GMT</lastBuildDate><pubDate>Fri, 02 Mar 2007 04:48:31 GMT</pubDate><ttl>60</ttl><item><title>1.11 DateUtils.truncate() </title><link>http://www.blogjava.net/borball/archive/2006/03/08/34276.html</link><dc:creator>borball的程序生涯</dc:creator><author>borball的程序生涯</author><pubDate>Wed, 08 Mar 2006 07:17:00 GMT</pubDate><guid>http://www.blogjava.net/borball/archive/2006/03/08/34276.html</guid><wfw:comment>http://www.blogjava.net/borball/comments/34276.html</wfw:comment><comments>http://www.blogjava.net/borball/archive/2006/03/08/34276.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/borball/comments/commentRss/34276.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/borball/services/trackbacks/34276.html</trackback:ping><description><![CDATA[<FONT size=2><FONT face="Courier New"><FONT color=#ff0000>Truncating Date Objects</FONT>(不知如何翻译,<IMG height=20 src="http://www.blogjava.net/Emoticons/QQ/11.gif" width=20 border=0>)，使用<FONT size=3> <TT><FONT color=#0000ff>DateUtils.truncate()</FONT></TT></FONT></FONT><A name=jakartackbk-CHP-1-ITERM-1886></A><FONT face="Courier New"><FONT color=#0000ff><FONT size=3> </FONT><BR></FONT>直接看例子吧<BR></FONT>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #0000ff"><FONT face="Courier New">import</FONT></SPAN><FONT face="Courier New"><SPAN style="COLOR: #000000">&nbsp;org.apache.commons.lang.time.DateUtils;<BR></SPAN><SPAN style="COLOR: #0000ff">import</SPAN></FONT><FONT face="Courier New"><SPAN style="COLOR: #000000">&nbsp;org.apache.commons.lang.time.FastDateFormat;<BR></SPAN><SPAN style="COLOR: #0000ff">import</SPAN></FONT><FONT face="Courier New"><SPAN style="COLOR: #000000">&nbsp;org.apache.commons.lang.time.DateFormatUtils;<BR><BR>FastDateFormat&nbsp;dtFormat&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN></FONT><FONT face="Courier New"><SPAN style="COLOR: #000000">&nbsp;DateFormatUtils.ISO_DATETIME_FORMAT;<BR><BR>Date&nbsp;now&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN></FONT><FONT face="Courier New"><SPAN style="COLOR: #000000">&nbsp;Date(&nbsp;);<BR><BR>Date&nbsp;truncatedMonth&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN></FONT><FONT face="Courier New"><SPAN style="COLOR: #000000">&nbsp;DateUtils.truncate(&nbsp;now,&nbsp;Calendar.MONTH&nbsp;);<BR>Date&nbsp;truncatedHour&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN></FONT><FONT face="Courier New"><SPAN style="COLOR: #000000">&nbsp;DateUtils.truncate(&nbsp;now,&nbsp;Calendar.HOUR&nbsp;);&nbsp;<BR><BR>System.out.println(&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Now:&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN></FONT><FONT face="Courier New"><SPAN style="COLOR: #000000">&nbsp;dtFormat.format(&nbsp;now&nbsp;)&nbsp;);<BR>System.out.println(&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Truncated&nbsp;Month:&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN></FONT><FONT face="Courier New"><SPAN style="COLOR: #000000">&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN></FONT><FONT face="Courier New"><SPAN style="COLOR: #000000">&nbsp;dtFormat.format(&nbsp;truncatedMonth&nbsp;)&nbsp;);<BR>System.out.println(&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Truncated&nbsp;Hour:&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN></FONT><FONT face="Courier New"><SPAN style="COLOR: #000000">&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN></FONT><SPAN style="COLOR: #000000"><FONT face="Courier New">&nbsp;dtFormat.format(&nbsp;truncatedHour&nbsp;)&nbsp;);<BR></FONT></SPAN></DIV>
<P><BR><FONT face="Courier New">输出为：Now: 2006-03-08T15:19:23<BR></FONT><FONT face="Courier New">Truncated Month: 2006-03-01T00:00:00<BR></FONT><FONT face="Courier New">Truncated Hour: 2006-03-08T15:00:00<BR><BR>由例子可以得知DateUtils.truncate()相当于舍弃我们不关心的Date fields。如某Date需要truncate到hour，则hour以后的fields如Calender.MINUTE,Calender.SECOND和Calender.MILLISECOND都将被舍弃。</FONT></FONT></P><img src ="http://www.blogjava.net/borball/aggbug/34276.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/borball/" target="_blank">borball的程序生涯</a> 2006-03-08 15:17 <a href="http://www.blogjava.net/borball/archive/2006/03/08/34276.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>1.10 DateUtils.round() </title><link>http://www.blogjava.net/borball/archive/2006/03/08/34273.html</link><dc:creator>borball的程序生涯</dc:creator><author>borball的程序生涯</author><pubDate>Wed, 08 Mar 2006 06:55:00 GMT</pubDate><guid>http://www.blogjava.net/borball/archive/2006/03/08/34273.html</guid><wfw:comment>http://www.blogjava.net/borball/comments/34273.html</wfw:comment><comments>http://www.blogjava.net/borball/archive/2006/03/08/34273.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/borball/comments/commentRss/34273.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/borball/services/trackbacks/34273.html</trackback:ping><description><![CDATA[<FONT color=#ff0000><FONT size=2>日期取整（日期精度调节，如调节至秒/分等）<BR></FONT><FONT color=#000000><FONT size=2>问题提出：日期取整<BR>解决方法：使用DateUtils。<FONT face="Courier New">DateUtils.round()</FONT></FONT><A name=jakartackbk-CHP-1-ITERM-1881></A><FONT face="Courier New" size=2> </FONT><A name=jakartackbk-CHP-1-ITERM-1882></A><FONT face="Courier New" size=2>can round to almost every&nbsp;Calender field,&nbsp;包括Calender.YEAR,Calendar.SECOND,Calendar.MINUTE,Calendar.HOUR,Calendar.DAY_OF_MONTH,Calendar.MONTH.<BR>使用举例：<BR><FONT color=#ff0000>&nbsp;</FONT> 
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;org.apache.commons.lang.time.FastDateFormat;<BR></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;org.apache.commons.lang.time.DateFormatUtils;<BR></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;org.apache.commons.lang.time.DateUtils;<BR><BR>FastDateFormat&nbsp;dtFormat&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;DateFormatUtils.ISO_DATETIME_FORMAT;<BR><BR>Date&nbsp;now&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Date(&nbsp;);<BR><BR>Date&nbsp;nearestHour&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;DateUtils.round(&nbsp;now,&nbsp;Calendar.HOUR&nbsp;);<BR>Date&nbsp;nearestDay&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;DateUtils.round(&nbsp;now,&nbsp;Calendar.DAY_OF_MONTH&nbsp;);<BR>Date&nbsp;nearestYear&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;DateUtils.round(&nbsp;now,&nbsp;Calendar.YEAR&nbsp;);<BR><BR>System.out.println(&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Now:&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;dtFormat.format(&nbsp;now&nbsp;)&nbsp;);<BR>System.out.println(&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Nearest&nbsp;Hour:&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;dtFormat.format(&nbsp;nearestHour&nbsp;)&nbsp;);<BR>System.out.println(&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Nearest&nbsp;Day:&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;dtFormat.format(&nbsp;nearestDay&nbsp;)&nbsp;);<BR>System.out.println(&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Nearest&nbsp;Year:&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;dtFormat.format(&nbsp;nearestYear&nbsp;)&nbsp;);</SPAN></DIV><BR>DateUtils.round()相当于数学中的四舍五入法取整，而DateUtils.truncate()相当与去余法取整。</FONT></FONT></FONT><img src ="http://www.blogjava.net/borball/aggbug/34273.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/borball/" target="_blank">borball的程序生涯</a> 2006-03-08 14:55 <a href="http://www.blogjava.net/borball/archive/2006/03/08/34273.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>1.9 FastDateFormat,DateFormatUtils</title><link>http://www.blogjava.net/borball/archive/2006/03/08/34212.html</link><dc:creator>borball的程序生涯</dc:creator><author>borball的程序生涯</author><pubDate>Wed, 08 Mar 2006 03:38:00 GMT</pubDate><guid>http://www.blogjava.net/borball/archive/2006/03/08/34212.html</guid><wfw:comment>http://www.blogjava.net/borball/comments/34212.html</wfw:comment><comments>http://www.blogjava.net/borball/archive/2006/03/08/34212.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/borball/comments/commentRss/34212.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/borball/services/trackbacks/34212.html</trackback:ping><description><![CDATA[<P><FONT color=#ff0000><FONT size=2>格式化日期<BR></FONT><FONT color=#000000><FONT size=2>问题提出：</FONT><FONT size=2><FONT face="Courier New">SimpleDateFormat是非线程安全的，而您又需要一个ISO格式的日期。<BR></FONT><FONT face="Times New Roman">解决方法：使用</FONT><FONT face="Courier New">FastDateFormat或者使用DateFormatUtils提供的静态FastDateFormat实例，它提供了一些格式化日期的线程安全的方法。<BR></FONT><FONT face="Times New Roman">使用举例：<BR></P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;now&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Date();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;isoDT&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(now);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">It&nbsp;is&nbsp;currently:&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;isoDT);</SPAN></DIV></FONT></FONT></FONT></FONT><FONT size=2>结果为It is currently: 2006-03-08T10:44:29+08:00<BR>如果您想使用传统的格式化类型，可以用</FONT><FONT face="Courier New"><FONT size=2>FastDateFormat来代替SimpleDateFormat，<BR></FONT>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><FONT size=2><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;create&nbsp;a&nbsp;formatter&nbsp;that&nbsp;simply&nbsp;prints&nbsp;the&nbsp;year&nbsp;and&nbsp;month</SPAN></FONT><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000"><BR><FONT size=2>FastDateFormat&nbsp;formatter&nbsp;</FONT></SPAN><SPAN style="COLOR: #000000"><FONT size=2>=</FONT></SPAN><FONT size=2><SPAN style="COLOR: #000000">&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;FastDateFormat(&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">yyyy-mm</SPAN><SPAN style="COLOR: #000000">"</SPAN></FONT><FONT size=2><SPAN style="COLOR: #000000">,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TimeZone.getDefault(&nbsp;),<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Locale.getDefault(&nbsp;)&nbsp;);<BR><BR>String&nbsp;output&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;formatter.format(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN></FONT><SPAN style="COLOR: #000000"><FONT size=2>&nbsp;Date(&nbsp;)&nbsp;);<BR><BR>System.out.println(output);<BR></FONT></SPAN></DIV><FONT size=2>在最新的版本中FastDateFormat的构造函数已经改为protected，统一使用getInstance()来获取FastDateFormat实例,上述的输出为2006-03.<BR>Discussion:In fact, you should be aware that none of the Sun formatting classes are thread-safe.</FONT><FONT size=3> </FONT><FONT size=2>以下是DateFormatUtils里面提供的Static date/time formats。<BR>
<TABLE cellSpacing=0 cellPadding=4 rules=all width="56%" border=1>
<THEAD>
<TR>
<TH class=thead>
<P class=docText>Name</P></TH>
<TH class=thead>
<P class=docText>Format</P></TH></TR></THEAD>
<TBODY>
<TR>
<TD class=docTableCell>
<P class=docText><TT>ISO_DATE_FORMAT</TT></P></TD>
<TD class=docTableCell>
<P class=docText><TT>yyyy-MM-dd"2004-01-02</TT>"</P></TD></TR>
<TR>
<TD class=docTableCell>
<P class=docText><TT>ISO_DATE_TIME_ZONE_FORMAT</TT></P></TD>
<TD class=docTableCell>
<P class=docText><TT>yyyy-MM-ddZZ"2004-01-02-07:00</TT>"</P></TD></TR>
<TR>
<TD class=docTableCell>
<P class=docText><TT>ISO_DATETIME_FORMAT</TT></P></TD>
<TD class=docTableCell>
<P class=docText><TT>yyyy-MM-dd'T'HH:mm:ss"2004-01-02T23:22:12</TT>"</P></TD></TR>
<TR>
<TD class=docTableCell>
<P class=docText><TT>ISO_DATETIME_TIME_ZONE_FORMAT</TT></P></TD>
<TD class=docTableCell>
<P class=docText><TT>yyyy-MM-dd'T'HH:mm:ssZZ"2004-01-02T21:13:45-07:00</TT>"</P></TD></TR>
<TR>
<TD class=docTableCell>
<P class=docText><TT>ISO_TIME_FORMAT</TT></P></TD>
<TD class=docTableCell>
<P class=docText>'<TT>T'HH:mm:ss"T04:23:22</TT>"</P></TD></TR>
<TR>
<TD class=docTableCell>
<P class=docText><TT>ISO_TIME_NO_T_FORMAT</TT></P></TD>
<TD class=docTableCell>
<P class=docText><TT>HH:mm:ss"05:12:34</TT>"</P></TD></TR>
<TR>
<TD class=docTableCell>
<P class=docText><TT>ISO_TIME_NO_T_TIME_ZONE_FORMAT</TT></P></TD>
<TD class=docTableCell>
<P class=docText><TT>HH:mm:ssZZ"12:32:22-07:00</TT>"</P></TD></TR>
<TR>
<TD class=docTableCell>
<P class=docText><TT>ISO_TIME_TIME_ZONE_FORMAT</TT></P></TD>
<TD class=docTableCell>
<P class=docText>'<TT>T'HH:mm:ssZZ"T18:23:22-07:00</TT>"</P></TD></TR>
<TR>
<TD class=docTableCell>
<P class=docText><TT>SMTP_DATETIME_FORMAT</TT></P></TD>
<TD class=docTableCell>
<P class=docText><TT>EEE, dd MMM yyyy HH:mm:ss Z"Wed, 01 Feb 2004 20:03:01 CST</TT>"</P></TD></TR></TBODY></TABLE></FONT></FONT><img src ="http://www.blogjava.net/borball/aggbug/34212.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/borball/" target="_blank">borball的程序生涯</a> 2006-03-08 11:38 <a href="http://www.blogjava.net/borball/archive/2006/03/08/34212.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>1.8 ArrayUtils.toMap() </title><link>http://www.blogjava.net/borball/archive/2006/03/08/34193.html</link><dc:creator>borball的程序生涯</dc:creator><author>borball的程序生涯</author><pubDate>Wed, 08 Mar 2006 02:05:00 GMT</pubDate><guid>http://www.blogjava.net/borball/archive/2006/03/08/34193.html</guid><wfw:comment>http://www.blogjava.net/borball/comments/34193.html</wfw:comment><comments>http://www.blogjava.net/borball/archive/2006/03/08/34193.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/borball/comments/commentRss/34193.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/borball/services/trackbacks/34193.html</trackback:ping><description><![CDATA[<FONT color=#ff0000 size=2>从多维数组里构造一个map<BR><FONT color=#000000>问题提出：需要构造一个和数组等价的map<BR>解决方法：使用<FONT face="Courier New">ArrayUtils.toMap()</FONT><A name=jakartackbk-CHP-1-ITERM-1863></A><FONT size=3> </FONT><FONT size=2>方法构造一个和二维数组(<FONT face="Courier New">object[]</FONT>)等价的map。<BR>使用举例：<BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;org.apache.commons.lang.ArrayUtils;<BR><BR>Object[]&nbsp;weightArray&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Object[][]&nbsp;{&nbsp;{</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">H</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Double(&nbsp;</SPAN><SPAN style="COLOR: #000000">1.007</SPAN><SPAN style="COLOR: #000000">)},<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">He</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Double(&nbsp;</SPAN><SPAN style="COLOR: #000000">4.002</SPAN><SPAN style="COLOR: #000000">)},<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Li</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Double(&nbsp;</SPAN><SPAN style="COLOR: #000000">6.941</SPAN><SPAN style="COLOR: #000000">)},<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Be</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Double(&nbsp;</SPAN><SPAN style="COLOR: #000000">9.012</SPAN><SPAN style="COLOR: #000000">)},<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">B</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Double(</SPAN><SPAN style="COLOR: #000000">10.811</SPAN><SPAN style="COLOR: #000000">)},<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">C</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Double(</SPAN><SPAN style="COLOR: #000000">12.010</SPAN><SPAN style="COLOR: #000000">)},<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">N</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Double(</SPAN><SPAN style="COLOR: #000000">14.007</SPAN><SPAN style="COLOR: #000000">)},<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">O</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Double(</SPAN><SPAN style="COLOR: #000000">15.999</SPAN><SPAN style="COLOR: #000000">)},<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">F</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Double(</SPAN><SPAN style="COLOR: #000000">18.998</SPAN><SPAN style="COLOR: #000000">)},<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Ne</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Double(</SPAN><SPAN style="COLOR: #000000">20.180</SPAN><SPAN style="COLOR: #000000">)}&nbsp;};<BR><BR></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Create&nbsp;a&nbsp;Map&nbsp;mapping&nbsp;colors.</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">Map&nbsp;weights&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;ArrayUtils.toMap(&nbsp;weightArray&nbsp;);<BR><BR>Double&nbsp;hydrogenWeight&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;map.get(&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">H</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;);<BR></SPAN></DIV></FONT></FONT></FONT><BR><FONT size=2>Discussion：<FONT face="Courier New">ArrayUtils.toMap()</FONT><A name=jakartackbk-CHP-1-ITERM-1865></A> 避免了反复调用put()方法去实现一个map，<FONT face="Courier New">ArrayUtils.toMap()</FONT><A name=jakartackbk-CHP-1-ITERM-1865></A> 会把<FONT face="Courier New">object[][]中每个元素(object[])的第一个元素当成key，第二个元素当成value.</FONT></FONT><img src ="http://www.blogjava.net/borball/aggbug/34193.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/borball/" target="_blank">borball的程序生涯</a> 2006-03-08 10:05 <a href="http://www.blogjava.net/borball/archive/2006/03/08/34193.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>1.7 ArrayUtils.contains() ,lastIndexOf(),lastIndexOf()</title><link>http://www.blogjava.net/borball/archive/2006/03/08/34187.html</link><dc:creator>borball的程序生涯</dc:creator><author>borball的程序生涯</author><pubDate>Wed, 08 Mar 2006 01:47:00 GMT</pubDate><guid>http://www.blogjava.net/borball/archive/2006/03/08/34187.html</guid><wfw:comment>http://www.blogjava.net/borball/comments/34187.html</wfw:comment><comments>http://www.blogjava.net/borball/archive/2006/03/08/34187.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/borball/comments/commentRss/34187.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/borball/services/trackbacks/34187.html</trackback:ping><description><![CDATA[<FONT color=#ff0000 size=2>数组中查找一个元素<BR><FONT color=#000000>问题提出：需要确定一个数组中是否含有某个元素并给出元素所在位置<BR>解决方法：使用<FONT face="Courier New">ArrayUtils.contains()</FONT><A name=jakartackbk-CHP-1-ITERM-1856></A><FONT size=3> </FONT>来确认是否含有，使用<FONT face="Courier New">ArrayUtils.lastIndexOf()和ArrayUtils.lastIndexOf()来确定该元素第一个出现的位置和最后一个出现的位置。<BR><BR>Discussion：ArrayUtils可以应用到所有的数组类型，如：boolean[]，byte[],char[],long[],double[],short[],int[]和object[].当是基本类型时ArrayUtils会比较各个元素的值。当时object[]时会调用equals()方法来找出以各匹配的对象。<BR>ArrayUtils.lastIndexOf()和ArrayUtils.lastIndexOf()还提供了第三个参数用来指定查找的开始位。<BR></FONT></FONT></FONT><img src ="http://www.blogjava.net/borball/aggbug/34187.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/borball/" target="_blank">borball的程序生涯</a> 2006-03-08 09:47 <a href="http://www.blogjava.net/borball/archive/2006/03/08/34187.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>1.6 ArrayUtils.toObject()和ArrayUtils.toPrimitive()</title><link>http://www.blogjava.net/borball/archive/2006/03/08/34183.html</link><dc:creator>borball的程序生涯</dc:creator><author>borball的程序生涯</author><pubDate>Wed, 08 Mar 2006 01:28:00 GMT</pubDate><guid>http://www.blogjava.net/borball/archive/2006/03/08/34183.html</guid><wfw:comment>http://www.blogjava.net/borball/comments/34183.html</wfw:comment><comments>http://www.blogjava.net/borball/archive/2006/03/08/34183.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/borball/comments/commentRss/34183.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/borball/services/trackbacks/34183.html</trackback:ping><description><![CDATA[<FONT color=#ff0000><FONT size=2>在对象数组(Object Arraysand )和基本类型数组(Primitive Arrays)间转换<BR></FONT><FONT color=#000000><FONT size=2>问题提出：需要在对象数组和基本类型数组间进行转换<BR>解决方法：使用<FONT face="Courier New">ArrayUtils.toObject()和ArrayUtils.toPrimitive()方法。<BR></FONT><FONT face="Times New Roman">使用举例：<BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;org.apache.commons.lang.ArrayUtils;<BR><BR></SPAN><SPAN style="COLOR: #0000ff">long</SPAN><SPAN style="COLOR: #000000">[]&nbsp;primitiveArray&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">long</SPAN><SPAN style="COLOR: #000000">[]&nbsp;{&nbsp;</SPAN><SPAN style="COLOR: #000000">12</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">100</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">2929</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">3323</SPAN><SPAN style="COLOR: #000000">&nbsp;};<BR>Long[]&nbsp;objectArray&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;ArrayUtils.toObject(&nbsp;primitiveArray&nbsp;);<BR><BR>Double[]&nbsp;doubleObjects&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Double[]&nbsp;{&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Double(&nbsp;</SPAN><SPAN style="COLOR: #000000">3.22</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">5.222</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">3.221</SPAN><SPAN style="COLOR: #000000">&nbsp;)&nbsp;};<BR><BR></SPAN><SPAN style="COLOR: #0000ff">double</SPAN><SPAN style="COLOR: #000000">[]&nbsp;doublePrimitives&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;ArrayUtils.toPrimitive(&nbsp;doubleObject&nbsp;);</SPAN></DIV><BR>Discussion：考虑以下场景：假设一个外部系统期望我们提供一个以Double对象为元素的List，而我们内部系统只有一个double[],以下代码可以使两者达到匹配。<BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Assume&nbsp;that&nbsp;temps&nbsp;is&nbsp;a&nbsp;4000&nbsp;element&nbsp;double[]</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff">double</SPAN><SPAN style="COLOR: #000000">[]&nbsp;temps&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;readTemps(&nbsp;);<BR><BR></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Turn&nbsp;the&nbsp;double[]&nbsp;into&nbsp;an&nbsp;array&nbsp;of&nbsp;Double&nbsp;objects</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">Double[]&nbsp;objectArray&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;ArrayUtils.toObject(&nbsp;temps&nbsp;);<BR>List&nbsp;inputList&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;Arrays.asList(&nbsp;objectArray&nbsp;);<BR><BR></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Assume&nbsp;that&nbsp;some&nbsp;process&nbsp;returns&nbsp;results&nbsp;as&nbsp;a&nbsp;List&nbsp;of&nbsp;Double&nbsp;<BR><BR></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;objects</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">List&nbsp;outputList&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;externalLib.complexCalculation(&nbsp;inputList&nbsp;);<BR><BR></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Transform&nbsp;this&nbsp;List&nbsp;of&nbsp;doubles&nbsp;to&nbsp;an&nbsp;array&nbsp;of&nbsp;double&nbsp;primitives</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">Double[]&nbsp;resultObjArray&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;(Double[])&nbsp;outputList.toArray(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Double[</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">]&nbsp;);<BR><BR></SPAN><SPAN style="COLOR: #0000ff">double</SPAN><SPAN style="COLOR: #000000">[]&nbsp;result&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;ArrayUtils.toPrimitive(&nbsp;resultObjArray,&nbsp;Double.NaN&nbsp;&nbsp;);<BR></SPAN></DIV></FONT></FONT></FONT></FONT><FONT size=2><FONT face="Courier New">ArrayUtils.toPrimitive()中对象数组可以为null,</FONT>最后一句double[] result =&nbsp;<BR>ArrayUtils.toPrimitive( resultObjArray, Double.NaN&nbsp; )指定当某元素为null的时候将用第二个参数代替。因为第二个参数是可选的，当不指定时将会抛出<FONT face="Courier New">NullPointerException异常。</FONT></FONT><img src ="http://www.blogjava.net/borball/aggbug/34183.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/borball/" target="_blank">borball的程序生涯</a> 2006-03-08 09:28 <a href="http://www.blogjava.net/borball/archive/2006/03/08/34183.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>1.5 ArrayUtils.clone() 和ArrayUtils.reverse() </title><link>http://www.blogjava.net/borball/archive/2006/03/08/34180.html</link><dc:creator>borball的程序生涯</dc:creator><author>borball的程序生涯</author><pubDate>Wed, 08 Mar 2006 01:01:00 GMT</pubDate><guid>http://www.blogjava.net/borball/archive/2006/03/08/34180.html</guid><wfw:comment>http://www.blogjava.net/borball/comments/34180.html</wfw:comment><comments>http://www.blogjava.net/borball/archive/2006/03/08/34180.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/borball/comments/commentRss/34180.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/borball/services/trackbacks/34180.html</trackback:ping><description><![CDATA[<FONT color=#ff0000><FONT size=2>clone并倒转一个数组<BR></FONT><FONT color=#000000><FONT size=2>问题提出：需要clone并倒转一个数组中的内容<BR>解决方法：使用<FONT face="Courier New">ArrayUtils.clone()</FONT></FONT><A name=jakartackbk-CHP-1-ITERM-1832></A><FONT size=2> 和</FONT><A name=jakartackbk-CHP-1-ITERM-1833></A><FONT size=2>&nbsp;<TT>ArrayUtils.reverse()</TT></FONT><A name=jakartackbk-CHP-1-ITERM-1834></A><FONT size=2> 方法<BR>使用举例：<BR></FONT>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><FONT size=2><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #0000ff">import</SPAN></FONT><FONT size=2><SPAN style="COLOR: #000000">&nbsp;org.apache.commons.lang.ArrayUtils;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #0000ff"><FONT color=#000000>&nbsp;&nbsp;&nbsp; </FONT>long</SPAN><SPAN style="COLOR: #000000">[]&nbsp;array&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN id=Codehighlighter1_59_78_Open_Text><SPAN style="COLOR: #000000">{&nbsp;</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">3</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">2</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">3</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">5</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">6</SPAN><SPAN style="COLOR: #000000">&nbsp;}</SPAN></SPAN></FONT><FONT size=2><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">long</SPAN><SPAN style="COLOR: #000000">[]&nbsp;reversed&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN></FONT><FONT size=2><SPAN style="COLOR: #000000">&nbsp;ArrayUtils.clone(&nbsp;array&nbsp;);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>ArrayUtils.reverse(&nbsp;reversed&nbsp;);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>System.out.println(&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Original:&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN></FONT><FONT size=2><SPAN style="COLOR: #000000">&nbsp;ArrayUtils.toString(&nbsp;array&nbsp;)&nbsp;);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>System.out.println(&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Reversed:&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;ArrayUtils.toString(&nbsp;reversed&nbsp;)&nbsp;);</SPAN></FONT></DIV></FONT></FONT><FONT size=2>输出的结果为：Original: { 1, 3, 2, 3, 5, 6 }&nbsp;&nbsp;&nbsp; Reversed: { 6, 5, 3, 2, 3, 1 }<BR>Discussion：在上述例子中ArrayUtils.clone()返回了一个新数组的引用，而ArrayUtils.reserve()方法会直接操作参数所指定的数组。除了基本类型外，这两个方法还支持对象数组。<BR>如果没有这两个方法的帮助，我们可能会用以下方法实现倒转。<BR></FONT>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><FONT size=2><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;readTemps&nbsp;returns&nbsp;a&nbsp;1000&nbsp;member&nbsp;double[]</SPAN></FONT><SPAN style="COLOR: #008000"><BR><FONT size=2><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></FONT></SPAN><SPAN style="COLOR: #000000"><BR><FONT size=2><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></FONT></SPAN><FONT size=2><SPAN style="COLOR: #0000ff">double</SPAN><SPAN style="COLOR: #000000">[]&nbsp;temperature&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN></FONT><FONT size=2><SPAN style="COLOR: #000000">&nbsp;readTemps(&nbsp;);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">double</SPAN><SPAN style="COLOR: #000000">[]&nbsp;reversed&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">double</SPAN></FONT><FONT size=2><SPAN style="COLOR: #000000">[temperature.length];<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #0000ff"><FONT color=#000000>&nbsp;&nbsp; </FONT>for</SPAN><SPAN style="COLOR: #000000">(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;temperature.length;&nbsp;i</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">&nbsp;)</SPAN></FONT><SPAN id=Codehighlighter1_181_239_Open_Text><FONT size=2><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;reversed[reversed.length&nbsp;</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">&nbsp;(i</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">)]&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN></FONT><SPAN style="COLOR: #000000"><FONT size=2>&nbsp;temperature[i];<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</FONT></SPAN></SPAN></DIV><FONT size=2>另外一种方法利用</FONT><FONT face="Courier New"><FONT size=2>Collections.reverse()，该方法是对一个List对象进行操作，所有之前需要将数组转换成List对象，另外List里面不能含有基本类型，还需要将基本类型包装一下，以下是实现的源码：<BR></FONT>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><FONT size=2><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #0000ff">double</SPAN><SPAN style="COLOR: #000000">[]&nbsp;temps&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN></FONT><FONT size=2><SPAN style="COLOR: #000000">&nbsp;readTemps(&nbsp;);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>List&nbsp;tempList&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN></FONT><FONT size=2><SPAN style="COLOR: #000000">&nbsp;ArrayList(&nbsp;);<BR></SPAN><SPAN style="COLOR: #0000ff"><FONT color=#000000>&nbsp;</FONT>for</SPAN><SPAN style="COLOR: #000000">(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;temps.length;&nbsp;i</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">&nbsp;)</SPAN></FONT><SPAN id=Codehighlighter1_106_150_Open_Text><FONT size=2><SPAN style="COLOR: #000000">{<BR>&nbsp;&nbsp;&nbsp;&nbsp;tempList.add(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN></FONT><SPAN style="COLOR: #000000"><FONT size=2>&nbsp;Double[temps[i]]&nbsp;);<BR>&nbsp;}</FONT></SPAN></SPAN><SPAN style="COLOR: #000000"><BR><FONT size=2><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>Collections.reverse(&nbsp;tempList&nbsp;);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></FONT></SPAN><FONT size=2><SPAN style="COLOR: #0000ff">double</SPAN><SPAN style="COLOR: #000000">[]&nbsp;reversed&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">double</SPAN></FONT><FONT size=2><SPAN style="COLOR: #000000">(&nbsp;tempList.size(&nbsp;)&nbsp;);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>Iterator&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN></FONT><FONT size=2><SPAN style="COLOR: #000000">&nbsp;tempList.iterator(&nbsp;);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;index&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN></FONT><FONT size=2><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #0000ff">&nbsp;while</SPAN><SPAN style="COLOR: #000000">(&nbsp;i.hasNext(&nbsp;)&nbsp;)</SPAN></FONT><SPAN id=Codehighlighter1_310_385_Open_Text><FONT size=2><SPAN style="COLOR: #000000">{<BR>&nbsp; &nbsp;&nbsp;reversed[index]&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN></FONT><FONT size=2><SPAN style="COLOR: #000000">&nbsp;((Double)&nbsp;i.next(&nbsp;)).doubleValue(&nbsp;);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;index</SPAN><SPAN style="COLOR: #000000">++</SPAN></FONT><SPAN style="COLOR: #000000"><FONT size=2>;<BR>&nbsp;}</FONT></SPAN></SPAN><SPAN style="COLOR: #000000"><BR><FONT size=2><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></FONT></SPAN></DIV></FONT><FONT size=2>有些人会问道：不是所有的数组都有一个clone()方法吗?一种可能的解释是ArrayUtils.clone()使程序更具可读性，另外当一个数组引用使null时，ArrayUtils.clone()会返回一个null而不是抛出一个运行时异常。</FONT><img src ="http://www.blogjava.net/borball/aggbug/34180.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/borball/" target="_blank">borball的程序生涯</a> 2006-03-08 09:01 <a href="http://www.blogjava.net/borball/archive/2006/03/08/34180.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>1.4 ArrayUtils.toString()</title><link>http://www.blogjava.net/borball/archive/2006/03/07/34122.html</link><dc:creator>borball的程序生涯</dc:creator><author>borball的程序生涯</author><pubDate>Tue, 07 Mar 2006 09:50:00 GMT</pubDate><guid>http://www.blogjava.net/borball/archive/2006/03/07/34122.html</guid><wfw:comment>http://www.blogjava.net/borball/comments/34122.html</wfw:comment><comments>http://www.blogjava.net/borball/archive/2006/03/07/34122.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/borball/comments/commentRss/34122.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/borball/services/trackbacks/34122.html</trackback:ping><description><![CDATA[<P><FONT size=2><FONT color=#ff0000>打印一个数组中的内容<BR></FONT>问题提出：如果需要打印数组中的内容<BR>解决方法：使用</FONT><FONT face="Courier New" size=2>ArrayUtils.toString()方法。<BR></FONT><FONT face="Times New Roman"><FONT size=2>使用举例：<BR></FONT></P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><FONT size=2><FONT color=#000000><SPAN style="COLOR: #0000ff">&nbsp;&nbsp;&nbsp; int</SPAN><SPAN style="COLOR: #000000">[]&nbsp;intArray&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">[]&nbsp;</SPAN><SPAN id=Codehighlighter1_27_43_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_27_43_Open_Text><SPAN style="COLOR: #000000">{&nbsp;</SPAN><SPAN style="COLOR: #000000">2</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">3</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">4</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">5</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">6</SPAN><SPAN style="COLOR: #000000">&nbsp;}</SPAN></SPAN></FONT></FONT><FONT size=2><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #0000ff"><FONT color=#000000>&nbsp;&nbsp;&nbsp; </FONT>int</SPAN><SPAN style="COLOR: #000000">[]&nbsp;multiDimension&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">[][]&nbsp;</SPAN><SPAN id=Codehighlighter1_82_117_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_82_117_Open_Text><SPAN style="COLOR: #000000">{&nbsp;</SPAN><SPAN id=Codehighlighter1_84_94_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_84_94_Open_Text><SPAN style="COLOR: #000000">{&nbsp;</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">2</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">3</SPAN><SPAN style="COLOR: #000000">&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN id=Codehighlighter1_97_104_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_97_104_Open_Text><SPAN style="COLOR: #000000">{&nbsp;</SPAN><SPAN style="COLOR: #000000">2</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">3</SPAN><SPAN style="COLOR: #000000">&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN id=Codehighlighter1_107_115_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_107_115_Open_Text><SPAN style="COLOR: #000000">{</SPAN><SPAN style="COLOR: #000000">5</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">6</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">7</SPAN><SPAN style="COLOR: #000000">}</SPAN></SPAN><SPAN style="COLOR: #000000">&nbsp;}</SPAN></SPAN></FONT><FONT size=2><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>System.out.println(&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">intArray:&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN></FONT><FONT size=2><SPAN style="COLOR: #000000">&nbsp;ArrayUtils.toString(&nbsp;intArray&nbsp;)&nbsp;);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>System.out.println(&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">multiDimension:&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN></FONT><SPAN style="COLOR: #000000"><FONT size=2>&nbsp;ArrayUtils.<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>toString(&nbsp;multiDimension&nbsp;)&nbsp;);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></FONT></SPAN></DIV></FONT><FONT size=2>程序输出结果如下：<BR>intArray: {2,3,4,5,6}<BR>multiDimension: {{1,2,3},{2,3},{5,6,7}}<BR><BR>Discussion：下面例子可以用来打印一个Object[]数组的所有内容，包括null。<BR></FONT>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><FONT size=2><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp; String[]&nbsp;strings&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;String[]&nbsp;</SPAN><SPAN id=Codehighlighter1_32_66_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_32_66_Open_Text><SPAN style="COLOR: #000000">{&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Blue</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Green</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Yellow</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;}</SPAN></SPAN></FONT><FONT size=2><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>System.out.println(&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Strings:&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;ArrayUtils.toString(&nbsp;strings,&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Unknown</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;);</SPAN></FONT></DIV><FONT size=2>当某一元素为null时，将会用ArrayUtils.toString( strings, "Unknown" )中第二个参数指定的String代替。</FONT><img src ="http://www.blogjava.net/borball/aggbug/34122.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/borball/" target="_blank">borball的程序生涯</a> 2006-03-07 17:50 <a href="http://www.blogjava.net/borball/archive/2006/03/07/34122.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>1.3 CompareToBuilder</title><link>http://www.blogjava.net/borball/archive/2006/03/07/34070.html</link><dc:creator>borball的程序生涯</dc:creator><author>borball的程序生涯</author><pubDate>Tue, 07 Mar 2006 06:45:00 GMT</pubDate><guid>http://www.blogjava.net/borball/archive/2006/03/07/34070.html</guid><wfw:comment>http://www.blogjava.net/borball/comments/34070.html</wfw:comment><comments>http://www.blogjava.net/borball/archive/2006/03/07/34070.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/borball/comments/commentRss/34070.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/borball/services/trackbacks/34070.html</trackback:ping><description><![CDATA[<P><FONT size=2>问题提出：需要快速实现compareTo()方法<BR>解决方法：使用<FONT face="Courier New"><FONT color=#ff0000>CompareToBuilder</FONT>提供的<FONT color=#ff0000>compareTo()</FONT>方法。同样的CompareToBuilder也使用了反射机制。以下代码提供了一个compareTo()方法，用于比较两个对象所有的非static和非transient成员变量。<BR></P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;org.apache.commons.lang.builder.CompareToBuilder;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Build&nbsp;a&nbsp;compareTo&nbsp;function&nbsp;from&nbsp;reflection&nbsp;</SPAN><SPAN style="COLOR: #008000"><BR><IMG id=Codehighlighter1_136_197_Open_Image onclick="this.style.display='none'; Codehighlighter1_136_197_Open_Text.style.display='none'; Codehighlighter1_136_197_Closed_Image.style.display='inline'; Codehighlighter1_136_197_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_136_197_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_136_197_Closed_Text.style.display='none'; Codehighlighter1_136_197_Open_Image.style.display='inline'; Codehighlighter1_136_197_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;compareTo(Object&nbsp;o)&nbsp;</SPAN><SPAN id=Codehighlighter1_136_197_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_136_197_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;CompareToBuilder.reflectionCompare(</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">,&nbsp;obj);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN></DIV></FONT></FONT><BR><FONT size=2>Discussion：</FONT><FONT face="Courier New"><FONT size=2>CompareToBuilder.reflectionCompare()提供了两个对象non-static和nontransient成员变量的方法。 reflectionCompare()方法不予理会static和transient变量，因此以下代码中的averageAge和fullName变量是不会进入比较表达式的。<BR></FONT>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><FONT size=2><IMG id=Codehighlighter1_32_420_Open_Image onclick="this.style.display='none'; Codehighlighter1_32_420_Open_Text.style.display='none'; Codehighlighter1_32_420_Closed_Image.style.display='inline'; Codehighlighter1_32_420_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_32_420_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_32_420_Closed_Text.style.display='none'; Codehighlighter1_32_420_Open_Image.style.display='inline'; Codehighlighter1_32_420_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;PoliticalCandidate&nbsp;</SPAN><SPAN id=Codehighlighter1_32_420_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN></FONT><SPAN id=Codehighlighter1_32_420_Open_Text><FONT size=2><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Static&nbsp;variable</SPAN></FONT><SPAN style="COLOR: #008000"><BR><FONT size=2><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></FONT></SPAN><FONT size=2><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN></FONT><FONT size=2><SPAN style="COLOR: #000000">&nbsp;String&nbsp;averageAge;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Member&nbsp;variables&nbsp;</SPAN></FONT><SPAN style="COLOR: #008000"><BR><FONT size=2><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></FONT></SPAN><FONT size=2><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">private</SPAN></FONT><FONT size=2><SPAN style="COLOR: #000000">&nbsp;String&nbsp;firstName;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">private</SPAN></FONT><FONT size=2><SPAN style="COLOR: #000000">&nbsp;String&nbsp;lastName;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">transient</SPAN></FONT><FONT size=2><SPAN style="COLOR: #000000">&nbsp;String&nbsp;fullName;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN></FONT><FONT size=2><SPAN style="COLOR: #008000">&nbsp;Constructors<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN></FONT><FONT size=2><SPAN style="COLOR: #008000">&nbsp;get/set&nbsp;methods<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Build&nbsp;a&nbsp;compareTo&nbsp;function&nbsp;from&nbsp;reflection&nbsp;</SPAN></FONT><SPAN style="COLOR: #008000"><BR><FONT size=2><IMG id=Codehighlighter1_349_417_Open_Image onclick="this.style.display='none'; Codehighlighter1_349_417_Open_Text.style.display='none'; Codehighlighter1_349_417_Closed_Image.style.display='inline'; Codehighlighter1_349_417_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_349_417_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_349_417_Closed_Text.style.display='none'; Codehighlighter1_349_417_Open_Image.style.display='inline'; Codehighlighter1_349_417_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top></FONT></SPAN><FONT size=2><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;compareTo(Object&nbsp;o)&nbsp;</SPAN><SPAN id=Codehighlighter1_349_417_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN></FONT><SPAN id=Codehighlighter1_349_417_Open_Text><FONT size=2><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;CompareToBuilder.reflectionCompare(</SPAN><SPAN style="COLOR: #0000ff">this</SPAN></FONT><SPAN style="COLOR: #000000"><FONT size=2>,&nbsp;obj);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</FONT></SPAN></SPAN><SPAN style="COLOR: #000000"><BR><FONT size=2><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</FONT></SPAN></SPAN></DIV></FONT><FONT size=2>比较对象成员变量的时候应该有一个比较的次序存在，上述代码中默认的应该是先比较lastName,然后是firstName。调用append()方法可以把要比较的变量加入比较表达式中，并且遵循后加入的先比较的次序。<BR>例如：<BR></FONT>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><FONT size=2><IMG id=Codehighlighter1_31_444_Open_Image onclick="this.style.display='none'; Codehighlighter1_31_444_Open_Text.style.display='none'; Codehighlighter1_31_444_Closed_Image.style.display='inline'; Codehighlighter1_31_444_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_31_444_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_31_444_Closed_Text.style.display='none'; Codehighlighter1_31_444_Open_Image.style.display='inline'; Codehighlighter1_31_444_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;compareTo(Object&nbsp;o)&nbsp;</SPAN><SPAN id=Codehighlighter1_31_444_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN></FONT><SPAN id=Codehighlighter1_31_444_Open_Text><FONT size=2><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;compare&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;By&nbsp;default&nbsp;return&nbsp;less-than</SPAN></FONT><SPAN style="COLOR: #008000"><BR><FONT size=2><IMG id=Codehighlighter1_173_421_Open_Image onclick="this.style.display='none'; Codehighlighter1_173_421_Open_Text.style.display='none'; Codehighlighter1_173_421_Closed_Image.style.display='inline'; Codehighlighter1_173_421_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_173_421_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_173_421_Closed_Text.style.display='none'; Codehighlighter1_173_421_Open_Image.style.display='inline'; Codehighlighter1_173_421_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top></FONT></SPAN><FONT size=2><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(&nbsp;o&nbsp;</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;PoliticalCandidate.</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">.isAssignableFrom(&nbsp;o.getClass(&nbsp;)&nbsp;)&nbsp;)&nbsp;</SPAN><SPAN id=Codehighlighter1_173_421_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN></FONT><SPAN id=Codehighlighter1_173_421_Open_Text><FONT size=2><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PoliticalCandidate&nbsp;pc&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN></FONT><FONT size=2><SPAN style="COLOR: #000000">&nbsp;(PoliticalCandidate)&nbsp;o;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;compare&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #0000ff">new</SPAN></FONT><SPAN style="COLOR: #000000"><FONT size=2>&nbsp;CompareToBuilder(&nbsp;)<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.append(firstName,&nbsp;pc.firstName)<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.append(lastName,&nbsp;pc.lastName)).toComparison(&nbsp;);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</FONT></SPAN></SPAN><SPAN style="COLOR: #000000"><BR><FONT size=2><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</FONT></SPAN><SPAN style="COLOR: #0000ff"><FONT size=2>return</FONT></SPAN><SPAN style="COLOR: #000000"><FONT size=2>&nbsp;compare;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</FONT></SPAN></SPAN></DIV><FONT size=2>在比较的时候会先比较lastName，只有在lastName相同的情况下才会比较firstName。<BR>ps：实现compareTo()的时候应保证和equals()规则相同，即当compareTo()返回是0的时候equals()应该返回true。<BR></FONT><img src ="http://www.blogjava.net/borball/aggbug/34070.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/borball/" target="_blank">borball的程序生涯</a> 2006-03-07 14:45 <a href="http://www.blogjava.net/borball/archive/2006/03/07/34070.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>1.2 EqualsBuilder和HashCodeBuilder</title><link>http://www.blogjava.net/borball/archive/2006/03/07/34017.html</link><dc:creator>borball的程序生涯</dc:creator><author>borball的程序生涯</author><pubDate>Tue, 07 Mar 2006 03:37:00 GMT</pubDate><guid>http://www.blogjava.net/borball/archive/2006/03/07/34017.html</guid><wfw:comment>http://www.blogjava.net/borball/comments/34017.html</wfw:comment><comments>http://www.blogjava.net/borball/archive/2006/03/07/34017.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/borball/comments/commentRss/34017.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/borball/services/trackbacks/34017.html</trackback:ping><description><![CDATA[<FONT size=2>2.自动化hashCode()和equals()<BR>&nbsp; 问题产生：当需要自动实现hashCode()和equals()方法<BR>&nbsp; 解决方法：使用<FONT face="Courier New">EqualsBuilder和</FONT></FONT><FONT size=2><TT>HashCodeBuilder</TT></FONT><A name=jakartackbk-CHP-1-ITERM-1797></A><FONT size=2> <BR>&nbsp; 使用举例：<BR>&nbsp; 
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;org.apache.commons.lang.builder.HashCodeBuilder;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;org.apache.commons.lang.builder.EqualsBuilder;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG id=Codehighlighter1_143_1005_Open_Image onclick="this.style.display='none'; Codehighlighter1_143_1005_Open_Text.style.display='none'; Codehighlighter1_143_1005_Closed_Image.style.display='inline'; Codehighlighter1_143_1005_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_143_1005_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_143_1005_Closed_Text.style.display='none'; Codehighlighter1_143_1005_Open_Image.style.display='inline'; Codehighlighter1_143_1005_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;PoliticalCandidate&nbsp;</SPAN><SPAN id=Codehighlighter1_143_1005_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_143_1005_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Member&nbsp;variables&nbsp;-&nbsp;omitted&nbsp;for&nbsp;brevity<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Constructors&nbsp;-&nbsp;omitted&nbsp;for&nbsp;brevity<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;get/set&nbsp;methods&nbsp;-&nbsp;omitted&nbsp;for&nbsp;brevity<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;A&nbsp;hashCode&nbsp;which&nbsp;creates&nbsp;a&nbsp;hash&nbsp;from&nbsp;the&nbsp;two&nbsp;unique&nbsp;identifiers</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000"><BR><IMG id=Codehighlighter1_377_524_Open_Image onclick="this.style.display='none'; Codehighlighter1_377_524_Open_Text.style.display='none'; Codehighlighter1_377_524_Closed_Image.style.display='inline'; Codehighlighter1_377_524_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_377_524_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_377_524_Closed_Text.style.display='none'; Codehighlighter1_377_524_Open_Image.style.display='inline'; Codehighlighter1_377_524_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;hashCode(&nbsp;)&nbsp;</SPAN><SPAN id=Codehighlighter1_377_524_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_377_524_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;HashCodeBuilder(</SPAN><SPAN style="COLOR: #000000">17</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">37</SPAN><SPAN style="COLOR: #000000">)<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.append(firstName)<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.append(lastName).toHashCode(&nbsp;);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;An&nbsp;equals&nbsp;which&nbsp;compares&nbsp;two&nbsp;unique&nbsp;identifiers</SPAN><SPAN style="COLOR: #008000"><BR><IMG id=Codehighlighter1_618_1002_Open_Image onclick="this.style.display='none'; Codehighlighter1_618_1002_Open_Text.style.display='none'; Codehighlighter1_618_1002_Closed_Image.style.display='inline'; Codehighlighter1_618_1002_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_618_1002_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_618_1002_Closed_Text.style.display='none'; Codehighlighter1_618_1002_Open_Image.style.display='inline'; Codehighlighter1_618_1002_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">boolean</SPAN><SPAN style="COLOR: #000000">&nbsp;equals(Object&nbsp;o)&nbsp;</SPAN><SPAN id=Codehighlighter1_618_1002_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_618_1002_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">boolean</SPAN><SPAN style="COLOR: #000000">&nbsp;equals&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">false</SPAN><SPAN style="COLOR: #000000">;<BR><IMG id=Codehighlighter1_736_973_Open_Image onclick="this.style.display='none'; Codehighlighter1_736_973_Open_Text.style.display='none'; Codehighlighter1_736_973_Closed_Image.style.display='inline'; Codehighlighter1_736_973_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_736_973_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_736_973_Closed_Text.style.display='none'; Codehighlighter1_736_973_Open_Image.style.display='inline'; Codehighlighter1_736_973_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;(&nbsp;o&nbsp;</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;&amp;</SPAN><SPAN style="COLOR: #000000">PoliticalCandidate.</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">.isAssignableFrom(o)&nbsp;)&nbsp;</SPAN><SPAN id=Codehighlighter1_736_973_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_736_973_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PoliticalCandidate&nbsp;pc&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(PoliticalCandidate)&nbsp;o;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;equals&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;EqualsBuilder(&nbsp;)<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.append(firstName,&nbsp;ps.firstName)<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.append(lastName,&nbsp;ps.lastName)).isEquals(&nbsp;);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;equals;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN></DIV>Discussion：<BR>1.在上述例子中，当有相同的firstname和lastname时,认为两个对象的hashCode相同，从而equals()返回true.<BR>如果hashCode取决于该class的所有filed时需要使用反射机制来产生一个hashCode。<BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG id=Codehighlighter1_23_78_Open_Image onclick="this.style.display='none'; Codehighlighter1_23_78_Open_Text.style.display='none'; Codehighlighter1_23_78_Closed_Image.style.display='inline'; Codehighlighter1_23_78_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_23_78_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_23_78_Closed_Text.style.display='none'; Codehighlighter1_23_78_Open_Image.style.display='inline'; Codehighlighter1_23_78_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;hashCode(&nbsp;)&nbsp;</SPAN><SPAN id=Codehighlighter1_23_78_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_23_78_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;HashCodeBuilder.reflectionHashCode(</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN></DIV><FONT face="Courier New">和ToStringBuilder</FONT>&nbsp;与 <TT>HashCodeBuilder一样</TT> <TT>EqualsBuilder</TT> 也是使用append()方法进行配置，<FONT face="Courier New">EqualsBuilder的append()方法可以接受基本类型、对象、数组作为参数。EqualsBuilder强大的地方在于可以直接把数组作为参数传入append()方法，EqualsBuilder会依次比较数组中的每个元素。<BR></FONT>2.如果两个对象相等当且仅当每个属性值都相等 这句话可以由以下代码实现：<BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG id=Codehighlighter1_32_86_Open_Image onclick="this.style.display='none'; Codehighlighter1_32_86_Open_Text.style.display='none'; Codehighlighter1_32_86_Closed_Image.style.display='inline'; Codehighlighter1_32_86_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_32_86_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_32_86_Closed_Text.style.display='none'; Codehighlighter1_32_86_Open_Image.style.display='inline'; Codehighlighter1_32_86_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">boolean</SPAN><SPAN style="COLOR: #000000">&nbsp;equals(Object&nbsp;o)&nbsp;</SPAN><SPAN id=Codehighlighter1_32_86_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_32_86_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;EqualsBuilder.reflectionEquals(</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">,&nbsp;o);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN></DIV></FONT><img src ="http://www.blogjava.net/borball/aggbug/34017.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/borball/" target="_blank">borball的程序生涯</a> 2006-03-07 11:37 <a href="http://www.blogjava.net/borball/archive/2006/03/07/34017.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>1.1 ReflectionToStringBuilder </title><link>http://www.blogjava.net/borball/archive/2006/03/07/34004.html</link><dc:creator>borball的程序生涯</dc:creator><author>borball的程序生涯</author><pubDate>Tue, 07 Mar 2006 02:51:00 GMT</pubDate><guid>http://www.blogjava.net/borball/archive/2006/03/07/34004.html</guid><wfw:comment>http://www.blogjava.net/borball/comments/34004.html</wfw:comment><comments>http://www.blogjava.net/borball/archive/2006/03/07/34004.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/borball/comments/commentRss/34004.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/borball/services/trackbacks/34004.html</trackback:ping><description><![CDATA[<FONT size=2>本笔记是在阅读<STRONG>Jakarta Commons Cookbook</STRONG>时所留下的。<BR></FONT><FONT color=#0000ff><FONT color=#000000 size=2>1.使用<FONT face="Courier New">ReflectionToStringBuilder</FONT> 或者<TT>ToStringBuilder</TT> 自动产生toString()的内容。<BR>&nbsp;&nbsp; 使用举例：假设有一个表征校长候选人信息的javabean－<FONT face="Courier New">PoliticalCandidate。<BR></FONT>&nbsp;&nbsp; 
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG id=Codehighlighter1_32_328_Open_Image onclick="this.style.display='none'; Codehighlighter1_32_328_Open_Text.style.display='none'; Codehighlighter1_32_328_Closed_Image.style.display='inline'; Codehighlighter1_32_328_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_32_328_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_32_328_Closed_Text.style.display='none'; Codehighlighter1_32_328_Open_Image.style.display='inline'; Codehighlighter1_32_328_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;PoliticalCandidate&nbsp;</SPAN><SPAN id=Codehighlighter1_32_328_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_32_328_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">&nbsp;String&nbsp;lastName;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">&nbsp;String&nbsp;firstName;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">&nbsp;Date&nbsp;dateOfBirth;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">&nbsp;BigDecimal&nbsp;moneyRaised;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">&nbsp;State&nbsp;homeState;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;get/set方法省略</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000"><BR><IMG id=Codehighlighter1_268_326_Open_Image onclick="this.style.display='none'; Codehighlighter1_268_326_Open_Text.style.display='none'; Codehighlighter1_268_326_Closed_Image.style.display='inline'; Codehighlighter1_268_326_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_268_326_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_268_326_Closed_Text.style.display='none'; Codehighlighter1_268_326_Open_Image.style.display='inline'; Codehighlighter1_268_326_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;toString(&nbsp;)&nbsp;</SPAN><SPAN id=Codehighlighter1_268_326_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_268_326_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ReflectionToStringBuilder.toString(&nbsp;</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">&nbsp;);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN></DIV><BR>该bean里面有个toString()方法，假设有以下操作：<BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Create&nbsp;a&nbsp;State</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">State&nbsp;va&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;State(&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">VA</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Virginia</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Create&nbsp;a&nbsp;Birth&nbsp;Date</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">Calendar&nbsp;calendar&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;GregorianCalendar(&nbsp;);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>calendar.set(&nbsp;Calendar.YEAR,&nbsp;</SPAN><SPAN style="COLOR: #000000">1743</SPAN><SPAN style="COLOR: #000000">&nbsp;);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>calendar.set(&nbsp;Calendar.MONTH,&nbsp;Calendar.APRIL&nbsp;);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>calendar.set(&nbsp;Calendar.DAY_OF_MONTH,&nbsp;</SPAN><SPAN style="COLOR: #000000">13</SPAN><SPAN style="COLOR: #000000">&nbsp;);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>Date&nbsp;dob&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;calendar.getTime(&nbsp;);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>BigDecimal&nbsp;moneyRaised&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;BigDecimal(&nbsp;</SPAN><SPAN style="COLOR: #000000">293829292.93</SPAN><SPAN style="COLOR: #000000">&nbsp;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Create&nbsp;a&nbsp;Political&nbsp;Candidate</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">PoliticalCandidate&nbsp;candidate&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;PoliticalCandidate(&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Jefferson</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Thomas</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;dob,&nbsp;moneyRaised,&nbsp;va&nbsp;);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>System.out.println(&nbsp;candidate&nbsp;);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN></DIV></FONT></FONT><FONT size=2>假设State对象也是一个使用<FONT face="Courier New">ReflectionToStringBuilder的javabean，上述程序一种可能的输出为<FONT color=#000000>com.discursive.jccook.lang.builders.PoliticalCandidate@187aeca</FONT></A><BR>&nbsp;&nbsp;&nbsp; [lastName=Jefferson,\firstName=Thomas,<BR>&nbsp;&nbsp;&nbsp;&nbsp; dateOfBirth=Sat Apr 13 22:38:42 CST 1743,<BR>&nbsp;&nbsp;&nbsp;&nbsp; moneyRaised=\293829292.930000007152557373046875,<BR>&nbsp;&nbsp;&nbsp;&nbsp; state=\com.discursive.jccook.lang.builders.State@87816d<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [abbreviation=VA,name=Virginia]]<BR><BR></FONT></FONT><img src ="http://www.blogjava.net/borball/aggbug/34004.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/borball/" target="_blank">borball的程序生涯</a> 2006-03-07 10:51 <a href="http://www.blogjava.net/borball/archive/2006/03/07/34004.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>