<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>fatkun&#039;s blog &#187; J2EE</title>
	<atom:link href="http://fatkun.com/category/j2ee/feed" rel="self" type="application/rss+xml" />
	<link>http://fatkun.com</link>
	<description>又一个 WordPress 站点</description>
	<lastBuildDate>Sun, 05 Feb 2012 15:21:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>org.hibernate.NonUniqueObjectException的原因与解决方法</title>
		<link>http://fatkun.com/2011/04/org-hibernate-nonuniqueobjectexception.html</link>
		<comments>http://fatkun.com/2011/04/org-hibernate-nonuniqueobjectexception.html#comments</comments>
		<pubDate>Fri, 01 Apr 2011 09:42:12 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[NonUniqueObjectException]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=859</guid>
		<description><![CDATA[使用hibernate更新对象时，出现如下错误： org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:[com.fatkun.dao.hibernate.User#12] 原因 在同一个session内，如果已经有一个对象已经是持久化状态(load进来等)，现在构造一个新的PO，和前一个持久化对象拥有相同的持久化标识(identifier)，在update的时候，就会抛这个错误。 举个例子（伪代码）： User user1 = session.load&#40;1&#41;; User user2 = new User&#40;&#41;; user2.setId&#40;1&#41;;//此时ID和user1一样 user2.setUsername&#40;&#34;fatkun&#34;&#41;; session.update&#40;user2&#41;;//这里会抛出错误 解决方法 1.不要重新new一个对象，使用load的对象对他进行更改值。 例如上面例子直接对user1操作，最后更新user1 2.如果是hibernate3以上，可以使用session.merge()方法 3.把session中同标识的对象移出(session.evict(user1))，使他成为脱管的状态，然后user2就可以update了。]]></description>
			<content:encoded><![CDATA[<p>使用hibernate更新对象时，出现如下错误：<br />
org.hibernate.NonUniqueObjectException: a different object with the  same identifier value was already associated with the session:[com.fatkun.dao.hibernate.User#12]</p>
<h2>原因</h2>
<p>在同一个session内，如果已经有一个对象已经是持久化状态(load进来等)，现在构造一个新的PO，和前一个持久化对象拥有相同的持久化标识(identifier)，在update的时候，就会抛这个错误。<br />
举个例子（伪代码）：</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">User user1 <span style="color: #339933;">=</span> session.<span style="color: #006633;">load</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
User user2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
user2.<span style="color: #006633;">setId</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//此时ID和user1一样</span>
user2.<span style="color: #006633;">setUsername</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fatkun&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
session.<span style="color: #006633;">update</span><span style="color: #009900;">&#40;</span>user2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//这里会抛出错误</span></pre></div></div>

<h2>解决方法</h2>
<p>1.不要重新new一个对象，使用load的对象对他进行更改值。<br />
例如上面例子直接对user1操作，最后更新user1<br />
2.如果是hibernate3以上，可以使用session.merge()方法<br />
3.把session中同标识的对象移出(session.evict(user1))，使他成为脱管的状态，然后user2就可以update了。</p>
]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2011/04/org-hibernate-nonuniqueobjectexception.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JDK源码分析：java.lang.String</title>
		<link>http://fatkun.com/2011/03/jdk-source-java-lang-string.html</link>
		<comments>http://fatkun.com/2011/03/jdk-source-java-lang-string.html#comments</comments>
		<pubDate>Thu, 10 Mar 2011 15:24:57 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[JAVA]]></category>
		<category><![CDATA[JDK源码分析]]></category>
		<category><![CDATA[String]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=831</guid>
		<description><![CDATA[最近开始看JDK源码，不能太懒了~~注释非常详细（虽然是英文），而且部分代码也不算很复杂。先挑的简单的看看。。为了坚持下去，所以在博客写些记录，一是为了记忆，二是给自己一个坚持的理由~~哇咔咔，英文不算很好，那就对着中文API一起看吧。。 String结构 这个类结构很简单。。 /** The value is used for character storage. */ private final char value&#91;&#93;; &#160; /** The offset is the first index of the storage that is used. */ private final int offset; &#160; /** The count is the number of characters in the String. */ private final int count; 用了一个char数组来存储字符，然后offset是偏移（这个我还搞不懂有啥用），count是String的长度。 注意到String类是final的，不可以被继承，而且private final [...]]]></description>
			<content:encoded><![CDATA[<p>最近开始看JDK源码，不能太懒了~~注释非常详细（虽然是英文），而且部分代码也不算很复杂。先挑的简单的看看。。为了坚持下去，所以在博客写些记录，一是为了记忆，二是给自己一个坚持的理由~~哇咔咔，英文不算很好，那就对着中文API一起看吧。。</p>
<h2>String结构</h2>
<p>这个类结构很简单。。</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">   <span style="color: #008000; font-style: italic; font-weight: bold;">/** The value is used for character storage. */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">char</span> value<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/** The offset is the first index of the storage that is used. */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> offset<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/** The count is the number of characters in the String. */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> count<span style="color: #339933;">;</span></pre></div></div>

<p>用了一个char数组来存储字符，然后offset是偏移（这个我还搞不懂有啥用），count是String的长度。<br />
注意到String类是final的，不可以被继承，而且private final char value[];，只能赋值一次，赋值后就不能变了，只有从新生成一个String对象。</p>
<h2>public String concat(String str)</h2>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> concat<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> str<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">int</span> otherLen <span style="color: #339933;">=</span> str.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>otherLen <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">char</span> buf<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span>count <span style="color: #339933;">+</span> otherLen<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	getChars<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, count, buf, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	str.<span style="color: #006633;">getChars</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, otherLen, buf, count<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, count <span style="color: #339933;">+</span> otherLen, buf<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>这段代码是连接两个String的，先拿个char数组当容器，从this和str分别取出char放入buf内，注意getChars方法的第四个参数，这个是说buf从count个开始拷贝。也就是说把两个string的char放在了一个char数组内，再返回一个新的String对象。(因为之前的String已经不可以改变)</p>
<h2>public int indexOf(String str, int fromIndex)</h2>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> indexOf<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> str, <span style="color: #000066; font-weight: bold;">int</span> fromIndex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> indexOf<span style="color: #009900;">&#40;</span>value, offset, count,
                       str.<span style="color: #006633;">value</span>, str.<span style="color: #006633;">offset</span>, str.<span style="color: #006633;">count</span>, fromIndex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//source源字符，sourceOffset源偏移，sourceCount源长度</span>
    <span style="color: #666666; font-style: italic;">//target查找的字符 ...</span>
    <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> indexOf<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> source, <span style="color: #000066; font-weight: bold;">int</span> sourceOffset, <span style="color: #000066; font-weight: bold;">int</span> sourceCount,
                       <span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> target, <span style="color: #000066; font-weight: bold;">int</span> targetOffset, <span style="color: #000066; font-weight: bold;">int</span> targetCount,
                       <span style="color: #000066; font-weight: bold;">int</span> fromIndex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//如果fromIndex比源字符还长(从0算起)，并且查找的字符长度为0，那就返回源字符的长度，否则返回-1</span>
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>fromIndex <span style="color: #339933;">&gt;=</span> sourceCount<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>targetCount <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">?</span> sourceCount <span style="color: #339933;">:</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
    	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>fromIndex <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    	    fromIndex <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    	<span style="color: #009900;">&#125;</span>
        <span style="color: #666666; font-style: italic;">//如果fromIndex比源字符短，查找的字符长度为0，直接返回fromIndex</span>
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>targetCount <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    <span style="color: #000000; font-weight: bold;">return</span> fromIndex<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//先取出第一个字符</span>
        <span style="color: #000066; font-weight: bold;">char</span> first  <span style="color: #339933;">=</span> target<span style="color: #009900;">&#91;</span>targetOffset<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span> max <span style="color: #339933;">=</span> sourceOffset <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>sourceCount <span style="color: #339933;">-</span> targetCount<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//循环每一个字符</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> sourceOffset <span style="color: #339933;">+</span> fromIndex<span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> max<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">/* 直到找到第一个字符 */</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>source<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> first<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">++</span>i <span style="color: #339933;">&lt;=</span> max <span style="color: #339933;">&amp;&amp;</span> source<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> first<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">/* 找到第一个字符后，比较剩下的字符 */</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">&lt;=</span> max<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> i <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">int</span> end <span style="color: #339933;">=</span> j <span style="color: #339933;">+</span> targetCount <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> k <span style="color: #339933;">=</span> targetOffset <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> end <span style="color: #339933;">&amp;&amp;</span> source<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span>
                         target<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span>, k<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>j <span style="color: #339933;">==</span> end<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #666666; font-style: italic;">/* 如果j能到end，那就说明找到整个字符串啦，返回偏移 */</span>
                    <span style="color: #000000; font-weight: bold;">return</span> i <span style="color: #339933;">-</span> sourceOffset<span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>indexOf只要看它的查找方法，先找到第一个，然后再匹配剩下的。</p>
<h2>public boolean equals(Object anObject)</h2>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> equals<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> anObject<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span> <span style="color: #339933;">==</span> anObject<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>anObject <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    <span style="color: #003399;">String</span> anotherString <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span>anObject<span style="color: #339933;">;</span>
	    <span style="color: #000066; font-weight: bold;">int</span> n <span style="color: #339933;">=</span> count<span style="color: #339933;">;</span>
	    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>n <span style="color: #339933;">==</span> anotherString.<span style="color: #006633;">count</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">char</span> v1<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> value<span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">char</span> v2<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> anotherString.<span style="color: #006633;">value</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> offset<span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> anotherString.<span style="color: #006633;">offset</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>n<span style="color: #339933;">--</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>v1<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> v2<span style="color: #009900;">&#91;</span>j<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	    <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>比较char~~</p>
<h2>replace与replaceAll</h2>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> replace<span style="color: #009900;">&#40;</span>CharSequence target, CharSequence replacement<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> Pattern.<span style="color: #006633;">compile</span><span style="color: #009900;">&#40;</span>target.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, Pattern.<span style="color: #006633;">LITERAL</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">matcher</span><span style="color: #009900;">&#40;</span>
            <span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">replaceAll</span><span style="color: #009900;">&#40;</span>Matcher.<span style="color: #006633;">quoteReplacement</span><span style="color: #009900;">&#40;</span>replacement.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> replaceAll<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> regex, <span style="color: #003399;">String</span> replacement<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">return</span> Pattern.<span style="color: #006633;">compile</span><span style="color: #009900;">&#40;</span>regex<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">matcher</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">replaceAll</span><span style="color: #009900;">&#40;</span>replacement<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>这两句真短啊。。用正则表达式来替换的。可以看出他们的区别。<br />
replace只支持普通字符的替换哦，Pattern.LITERAL是指启用模式的字面值解析。Matcher.quoteReplacement(str)返回指定 String 的字面值替换 String。这个方法也就是替换\\为\\\\(四个反斜杠)，$换为\\$。这样，它就不能用正则表达式了，但是看到依然是replaceAll，替换所有。<br />
replaceAll很简单的一个正则表达式使用~~<br />
要注意的是源字符串替换后内容并没有发生变化。<br />
举例如下: <a href="http://satellite.javaeye.com/blog/224820">来源</a><br />
        String src = new String(“ab43a2c43d”);<br />
        System.out.println(src.replace(“3&#8243;,”f”));=>ab4f2c4fd.<br />
        System.out.println(src.replace(&#8217;3&#8242;,&#8217;f'));=>ab4f2c4fd.<br />
        System.out.println(src.replaceAll(“\\d”,”f”));=>abffafcffd.<br />
        System.out.println(src.replaceAll(“a”,”f”));=>fb43fc23d.<br />
        System.out.println(src.replaceFirst(“\\d,”f”));=>abf32c43d<br />
        System.out.println(src.replaceFirst(“4&#8243;,”h”));=>abh32c43d.</p>
]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2011/03/jdk-source-java-lang-string.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>SQL Server JDBC Driver 2.0编译失败</title>
		<link>http://fatkun.com/2011/01/sql-server-jdbc-driver-2-0%e7%bc%96%e8%af%91%e5%a4%b1%e8%b4%a5.html</link>
		<comments>http://fatkun.com/2011/01/sql-server-jdbc-driver-2-0%e7%bc%96%e8%af%91%e5%a4%b1%e8%b4%a5.html#comments</comments>
		<pubDate>Thu, 27 Jan 2011 04:46:59 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[jdk]]></category>
		<category><![CDATA[版本错误]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=790</guid>
		<description><![CDATA[&#160; Exception in thread &#34;main&#34; java.lang.UnsupportedClassVersionError: Bad version number in .class file 这个错误是因为JDK的版本不对，请检查你的JDK版本。然后检查你的包是不是都用低版本JDK编译的。 如果你在使用JDK1.5的话，一定要使用sqljdbc.jar包，sqljdbc4.jar是在JDK1.6的环境下才能使用的。 &#160;]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<p align="left">Exception in thread &quot;main&quot; java.lang.UnsupportedClassVersionError: Bad version number in .class file</p>
<p align="left">这个错误是因为JDK的版本不对，请检查你的JDK版本。然后检查你的包是不是都用低版本JDK编译的。</p>
<p align="left">如果你在使用JDK1.5的话，一定要使用sqljdbc.jar包，sqljdbc4.jar是在JDK1.6的环境下才能使用的。</p>
<p align="left">&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2011/01/sql-server-jdbc-driver-2-0%e7%bc%96%e8%af%91%e5%a4%b1%e8%b4%a5.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JFreeChart核心类库介绍</title>
		<link>http://fatkun.com/2010/12/jfreechart%e6%a0%b8%e5%bf%83%e7%b1%bb%e5%ba%93.html</link>
		<comments>http://fatkun.com/2010/12/jfreechart%e6%a0%b8%e5%bf%83%e7%b1%bb%e5%ba%93.html#comments</comments>
		<pubDate>Fri, 31 Dec 2010 05:54:44 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[jfreechart]]></category>
		<category><![CDATA[XY轴]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=765</guid>
		<description><![CDATA[以下内容包括了如何调整XY轴数值间隔 原文来自：http://hi.baidu.com/baileyfu/blog/item/8e75b212b47fea54f819b808.html jfreechart主要由两个大的包组成：org.jfree.chart,org.jfree.data。其中前者主要与图形 &#160; 本身有关，后者与图形显示的数据有关。 &#160; 核心类主要有： &#160; org.jfree.chart.JFreeChart：图表对象，任何类型的图表的最终表现形式都是在该对象进行一些属性的定制。JFreeChart引擎本身提供了一个工厂类用于创建不同类型的图表对象 &#160; org.jfree.data.category.XXXDataSet:数据集对象，用于提供显示图表所用的数据。根据不同类型的图表对应着很多类型的数据集对象类 &#160; org.jfree.chart.plot.XXXPlot：图表区域对象，基本上这个对象决定着什么样式的图表，创建该对象的时候需要Axis、Renderer以及数据集对象的支持 &#160; org.jfree.chart.axis.XXXAxis：用于处理图表的两个轴：纵轴和横轴 &#160; org.jfree.chart.render.XXXRender：负责如何显示一个图表对象 &#160; org.jfree.chart.urls.XXXURLGenerator:用于生成Web图表中每个项目的鼠标点击链接 &#160; XXXXXToolTipGenerator:用于生成图象的帮助提示，不同类型图表对应不同类型的工具提示类 &#160; 对于常用的饼图阖柱状图，比较简单而且网上有很多的文章介绍，在这里就不再一一复述了， &#160; 主要说明下另一种常见的报表，时序图，首先声明一个曲线数据集合对象和曲线对象 &#160; TimePeriodValuesCollection timeseriescollection = new TimePeriodValuesCollection(); &#160; //声明具体是曲线对象，(可根据实际情况在同一张图中显示多条曲线进行数据比对，根据实际应用情况当超过4条曲线时，就会有些乱。) &#160; TimePeriodValues timeperiod1 = new TimePeriodValues(&#34;服务器A在线用户数量&#34;); &#160; TimePeriodValues timeperiod2 = new TimePeriodValues(&#34;服务器B在线用户数量&#34;); &#160; 我在使用TimeSeriesCollection tsc = new TimeSeriesCollection(); &#160; TimeSeries ts = [...]]]></description>
			<content:encoded><![CDATA[<p>以下内容包括了如何调整XY轴数值间隔</p>
<p>原文来自：<a href="http://hi.baidu.com/baileyfu/blog/item/8e75b212b47fea54f819b808.html">http://hi.baidu.com/baileyfu/blog/item/8e75b212b47fea54f819b808.html</a></p>
<p><span id="more-765"></span></p>
<p>jfreechart主要由两个大的包组成：org.jfree.chart,org.jfree.data。其中前者主要与图形</p>
<div>&nbsp;</div>
<div>本身有关，后者与图形显示的数据有关。</div>
<div>&nbsp;</div>
<h2>核心类主要有：</h2>
<div>&nbsp;</div>
<div>org.jfree.chart.JFreeChart：图表对象，任何类型的图表的最终表现形式都是在该对象进行一些属性的定制。JFreeChart引擎本身提供了一个工厂类用于创建不同类型的图表对象</div>
<div>&nbsp;</div>
<div>org.jfree.data.category.XXXDataSet:数据集对象，用于提供显示图表所用的数据。根据不同类型的图表对应着很多类型的数据集对象类</div>
<div>&nbsp;</div>
<div>org.jfree.chart.plot.XXXPlot：图表区域对象，基本上这个对象决定着什么样式的图表，创建该对象的时候需要Axis、Renderer以及数据集对象的支持</div>
<div>&nbsp;</div>
<div>org.jfree.chart.axis.XXXAxis：用于处理图表的两个轴：纵轴和横轴</div>
<div>&nbsp;</div>
<div>org.jfree.chart.render.XXXRender：负责如何显示一个图表对象</div>
<div>&nbsp;</div>
<div>org.jfree.chart.urls.XXXURLGenerator:用于生成Web图表中每个项目的鼠标点击链接</div>
<div>&nbsp;</div>
<div>XXXXXToolTipGenerator:用于生成图象的帮助提示，不同类型图表对应不同类型的工具提示类</div>
<div>&nbsp;</div>
<div>对于常用的饼图阖柱状图，比较简单而且网上有很多的文章介绍，在这里就不再一一复述了，</div>
<div>&nbsp;</div>
<div>主要说明下另一种常见的报表，时序图，首先声明一个曲线数据集合对象和曲线对象</div>
<div>&nbsp;</div>
<div>TimePeriodValuesCollection timeseriescollection = new TimePeriodValuesCollection();</div>
<div>&nbsp;</div>
<div>//声明具体是曲线对象，(可根据实际情况在同一张图中显示多条曲线进行数据比对，根据实际应用情况当超过4条曲线时，就会有些乱。)</div>
<div>&nbsp;</div>
<div>TimePeriodValues timeperiod1 = new TimePeriodValues(&quot;服务器A在线用户数量&quot;);</div>
<div>&nbsp;</div>
<div>TimePeriodValues timeperiod2 = new TimePeriodValues(&quot;服务器B在线用户数量&quot;);</div>
<div>&nbsp;</div>
<div>我在使用TimeSeriesCollection tsc = new TimeSeriesCollection();</div>
<div>&nbsp;</div>
<div>TimeSeries ts = new TimeSeries();</div>
<div>&nbsp;</div>
<div>在生成数据集时（ts.add(new Day(day, month, year),10))）只能生成最小单位为天的横轴所以改用了TimePeriodValuesCollection</div>
<div>&nbsp;</div>
<div>//根据当前时间取得横轴坐标，时间间隔为1小时</div>
<div>&nbsp;</div>
<div>Calendar cal = Calendar.getInstance();</div>
<div>&nbsp;</div>
<div>int year = cal.get(Calendar.YEAR);</div>
<div>&nbsp;</div>
<div>int month = cal.get(Calendar.MONTH) + 1;</div>
<div>&nbsp;</div>
<div>int day = cal.get(Calendar.DAY_OF_MONTH);</div>
<div>&nbsp;</div>
<div>//这里改为根据自己程序得到的需要显示的时间点和对应的数据的集合;</div>
<div>&nbsp;</div>
<div>List objectList1 = dao.getList1();</div>
<div>&nbsp;</div>
<div>List objectList2 = dao.getList2();</div>
<div>&nbsp;</div>
<div>//使用循环，把x轴，y轴的值赋给timeseries1</div>
<div>&nbsp;</div>
<div>for (int i =0;i&lt;objecthash1.size();i++) {</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp; &nbsp; int hour = objecthash1[i].getHours();</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp; &nbsp; int count = objecthash1[i].getCount();</div>
<div>&nbsp;</div>
<div>//将每一对数据（时间，数值）添加到数据集合1(曲线对象1)中</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp; &nbsp; timeseries1.add(new Hour(hour, day, month, year),count);</div>
<div>&nbsp;</div>
<div>}</div>
<div>&nbsp;</div>
<div>for (int i =0;i&lt;objecthash2.size();i++) {</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp; &nbsp; int hour = objecthash2[i].getHours();</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp; &nbsp; int count = objecthash2[i].getCount();</div>
<div>&nbsp;</div>
<div>//将每一对数据（时间，数值）添加到数据集合2(曲线对象2)中</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp; &nbsp; timeseries2.add(new Hour(hour, day, month, year),count);</div>
<div>&nbsp;</div>
<div>}</div>
<div>&nbsp;</div>
<div>//将曲线对象添加到曲线数据集合对象中</div>
<div>&nbsp;</div>
<div>timeseriescollection.addSeries(timeseries1);</div>
<div>&nbsp;</div>
<div>timeseriescollection.addSeries(timeseries2);</div>
<div>&nbsp;</div>
<div>//绘制报表</div>
<div>&nbsp;</div>
<div>String title = &quot;日在线用户统计&quot;; //报表标题</div>
<div>&nbsp;</div>
<div>String domain = &quot;时间&quot;; &nbsp; &nbsp; //x轴</div>
<div>&nbsp;</div>
<div>String range = &quot;用户在线数量&quot;; &nbsp; &nbsp;//y轴</div>
<div>&nbsp;</div>
<div>//创建时间序列图对象</div>
<div>&nbsp;</div>
<div>JFreeChart chart = ChartFactory.createTimeSeriesChart(</div>
<div>&nbsp;</div>
<div>title, &nbsp; //报表标题</div>
<div>&nbsp;</div>
<div>domain, //报表横轴标签</div>
<div>&nbsp;</div>
<div>range, &nbsp; //报表纵轴标签</div>
<div>&nbsp;</div>
<div>timeseriescollection, //数据集合</div>
<div>&nbsp;</div>
<div>true, &nbsp; //是否显示图例,在这里如果为true则会在图表的下方显示各条数据曲线的名称和颜色</div>
<div>&nbsp;</div>
<div>false, // 是否生成工具</div>
<div>&nbsp;</div>
<div>false &nbsp; &nbsp;// 是否生成URL链接);</div>
<div>&nbsp;</div>
<div>//将报表保存为jpg文件</div>
<div>&nbsp;</div>
<div>ChartUtilities.saveChartAsJPEG(file, //文件保存物理路径包括路径和文件名</div>
<div>&nbsp;</div>
<div>100, &nbsp; &nbsp;//图片质量</div>
<div>&nbsp;</div>
<div>chart, //图表对象</div>
<div>&nbsp;</div>
<div>1024, &nbsp; //图像宽度</div>
<div>&nbsp;</div>
<div>768, &nbsp; &nbsp;//图像高度</div>
<div>&nbsp;</div>
<div>null); //显示信息</div>
<div>&nbsp;</div>
<div>//将报表直接在页面输出</div>
<div>&nbsp;</div>
<div>ChartUtilities.writeChartAsJPEG(res.getOutputStream(),100,chart,1024,768,null);</div>
<div>&nbsp;</div>
<div>String title=&quot;月在线用户统计&quot;; &nbsp; //标题</div>
<div>&nbsp;</div>
<div>String domain=&quot;时间(天)&quot;;//x轴</div>
<div>&nbsp;</div>
<div>String range=&quot;用户在线数量&quot;;//y轴</div>
<div>&nbsp;</div>
<div>TimePeriodValuesCollection &nbsp; &nbsp;timeseriescollection &nbsp; &nbsp;= &nbsp; &nbsp;new &nbsp; &nbsp;TimePeriodValuesCollection();</div>
<div>&nbsp;</div>
<div>TimePeriodValues timeseries = new TimePeriodValues( &quot;用户数量&quot;);</div>
<div>&nbsp;</div>
<div>timeseries.add(new Minute(0, 1, 1, 1, 2006), 100);</div>
<div>&nbsp;</div>
<div>timeseries.add(new Minute(10, 1, 1, 1, 2006), 500);</div>
<div>&nbsp;</div>
<div>timeseries.add(new Minute(20, 1, 1, 1, 2006), 300);</div>
<div>&nbsp;</div>
<div>timeseries.add(new Minute(30, 1, 1, 1, 2006), 800);</div>
<div>&nbsp;</div>
<div>JFreeChart chart =ChartFactory.createTimeSeriesChart(title,domain,range,timeseriescollection,true,false,false);</div>
<div>&nbsp;</div>
<div>当我们生成了一个报表对象时，可能需要根据实际情况来决定报表的横轴和纵轴的数值间隔，显示方式等。</div>
<div>&nbsp;</div>
<div>可以用XYPlot xyplot = (XYPlot)chart.getPlot();来得到所有数据点的集合。（其它形状图表得到的数据集对象根据实际情况造型）</div>
<div>&nbsp;</div>
<div>得到数据点集合后，我们就可以设置各条曲线的颜色，和坐标轴的距离，x轴、y轴的显示方式等等属性</div>
<div>&nbsp;</div>
<div>xyplot.setBackgroundPaint(Color.lightGray); //设定图表数据显示部分背景色</div>
<div>&nbsp;</div>
<div>xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D)); //设定坐标轴与图表数据显示部分距离</div>
<div>&nbsp;</div>
<div>xyplot.setDomainGridlinePaint(Color.white); //网格线纵向颜色</div>
<div>&nbsp;</div>
<div>xyplot.setRangeGridlinePaint(Color.white); //网格线横向颜色</div>
<div>&nbsp;</div>
<h2>数据点的调整</h2>
<div>&nbsp;</div>
<div>XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)xyplot.getRenderer();</div>
<div>&nbsp;</div>
<div>xylineandshaperenderer.setDefaultShapesVisible(true); &nbsp; //数据点可见</div>
<div>&nbsp;</div>
<div>xylineandshaperenderer.setSeriesFillPaint(0, Color.red); &nbsp; //设置第一条曲线数据点填充为红色，如果一个图表有多条曲线可分别设置</div>
<div>&nbsp;</div>
<div>xylineandshaperenderer.setUseFillPaint(true); &nbsp; &nbsp; //应用</div>
<div>&nbsp;</div>
<div>使用xyplot.getRangeAxis()得到纵轴，xyplot.getDomainAxis()得到横轴，得到后可以根据实际情况造型为自己所需要的类型。</div>
<div>&nbsp;</div>
<div>我的图表纵轴为数值类型，横轴为时间类型，使用如下方式</div>
<div>&nbsp;</div>
<div>NumberAxis numAxis = (NumberAxis)xyplot.getRangeAxis();</div>
<div>&nbsp;</div>
<div>DateAxis &nbsp; dateaxis = &nbsp; &nbsp;(DateAxis)xyplot.getDomainAxis();</div>
<div>&nbsp;</div>
<div>//设置y显示方式</div>
<div>&nbsp;</div>
<div>numAxis.setAutoTickUnitSelection(false);//数据轴的数据标签是否自动确定</div>
<div>&nbsp;</div>
<div>double &nbsp; rangetick = 0.1D;</div>
<div>&nbsp;</div>
<div>numAxis.setTickUnit(new NumberTickUnit(rangetick)); &nbsp; //y轴单位间隔为0.1</div>
<div>&nbsp;</div>
<div>//设置x轴显示方式</div>
<div>&nbsp;</div>
<div>dateaxis.setAutoTickUnitSelection(false);//数据轴的数据标签是否自动确定</div>
<div>&nbsp;</div>
<div>dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.DAY,1));//x轴单位间隔为1天</div>
<div>&nbsp;</div>
<div>我们还可以是将数据格式化以后显示，比如y轴显示百分比（10%～100%）,x轴显示为&times;月&times;日</div>
<div>&nbsp;</div>
<div>NumberFormat nf =NumberFormat.getPercentInstance();</div>
<div>&nbsp;</div>
<div>numAxis.setNumberFormatOverride(nf);//设置y轴以百分比方式显示</div>
<div>&nbsp;</div>
<div>SimpleDateFormat format = new SimpleDateFormat(&quot;MM月dd&quot;);</div>
<div>&nbsp;</div>
<div>dateaxis.setDateFormatOverride(format);//设置x轴数据单位以&times;月&times;日方式显示</div>
<div>&nbsp;</div>
<div>时序图中还有一个很重要的方法</div>
<div>&nbsp;</div>
<div>timeseriescollection.setDomainIsPointsInTime(true); //x轴上的刻度点代表的是时间点而不是时间段</div>
<div>&nbsp;</div>
<div>最开始我没有设置这个属性,结果画出来的图，老是差半格不能在这个刻度的时候准确显示，往后移了半格，就是因为JFreeChart默认这个刻度是</div>
<div>&nbsp;</div>
<div>一个时间段，它把这个刻度和下个刻度的中间点认为是显示数据点最佳位置。</div>
<div>&nbsp;</div>
<div>其他一些关于AXIS类的方法：</div>
<div>&nbsp;</div>
<h2>Axis类：</h2>
<div>&nbsp;</div>
<div>void setVisible(boolean flag)坐标轴是否可见</div>
<div>&nbsp;</div>
<div>void setAxisLinePaint(Paint paint)坐标轴线条颜色（3D轴无效）</div>
<div>&nbsp;</div>
<div>void setAxisLineStroke(Stroke stroke)坐标轴线条笔触（3D轴无效）</div>
<div>&nbsp;</div>
<div>void setAxisLineVisible(boolean visible)坐标轴线条是否可见（3D轴无效）</div>
<div>&nbsp;</div>
<div>void setFixedDimension(double dimension)（用于复合表中对多坐标轴的设置）</div>
<div>&nbsp;</div>
<div>void setLabel(String label)坐标轴标题</div>
<div>&nbsp;</div>
<div>void setLabelFont(Font font)坐标轴标题字体</div>
<div>&nbsp;</div>
<div>void setLabelPaint(Paint paint)坐标轴标题颜色</div>
<div>&nbsp;</div>
<div>void setLabelAngle(double angle)`坐标轴标题旋转角度（纵坐标可以旋转）</div>
<div>&nbsp;</div>
<div>void setTickLabelFont(Font font)坐标轴标尺值字体</div>
<div>&nbsp;</div>
<div>void setTickLabelPaint(Paint paint)坐标轴标尺值颜色</div>
<div>&nbsp;</div>
<div>void setTickLabelsVisible(boolean flag)坐标轴标尺值是否显示</div>
<div>&nbsp;</div>
<div>void setTickMarkPaint(Paint paint)坐标轴标尺颜色</div>
<div>&nbsp;</div>
<div>void setTickMarkStroke(Stroke stroke)坐标轴标尺笔触</div>
<div>&nbsp;</div>
<div>void setTickMarksVisible(boolean flag)坐标轴标尺是否显示</div>
<div>&nbsp;</div>
<div>ValueAxis(Axis)类：</div>
<div>&nbsp;</div>
<div>void setAutoRange(boolean auto)自动设置数据轴数据范围</div>
<div>&nbsp;</div>
<div>void setAutoRangeMinimumSize(double size)自动设置数据轴数据范围时数据范围的最小跨度</div>
<div>&nbsp;</div>
<div>void setAutoTickUnitSelection(boolean flag)数据轴的数据标签是否自动确定（默认为true）</div>
<div>&nbsp;</div>
<div>void setFixedAutoRange(double length)数据轴固定数据范围（设置100的话就是显示MAXVALUE到MAXVALUE-100那段数据范围）</div>
<div>&nbsp;</div>
<div>void setInverted(boolean flag)数据轴是否反向（默认为false）</div>
<div>&nbsp;</div>
<div>void setLowerMargin(double margin)数据轴下（左）边距</div>
<div>&nbsp;</div>
<div>void setUpperMargin(double margin)数据轴上（右）边距</div>
<div>&nbsp;</div>
<div>void setLowerBound(double min)数据轴上的显示最小值</div>
<div>&nbsp;</div>
<div>void setUpperBound(double max)数据轴上的显示最大值</div>
<div>&nbsp;</div>
<div>void setPositiveArrowVisible(boolean visible)是否显示正向箭头（3D轴无效）</div>
<div>&nbsp;</div>
<div>void setNegativeArrowVisible(boolean visible)是否显示反向箭头（3D轴无效）</div>
<div>&nbsp;</div>
<div>void setVerticalTickLabels(boolean flag)数据轴数据标签是否旋转到垂直</div>
<div>&nbsp;</div>
<div>void setStandardTickUnits(TickUnitSource source)数据轴的数据标签（可以只显示整数标签，需要将AutoTickUnitSelection设false）</div>
<div>&nbsp;</div>
<div>NumberAxis(ValueAxis)类：</div>
<div>&nbsp;</div>
<div>void setAutoRangeIncludesZero(boolean flag)是否强制在自动选择的数据范围中包含0</div>
<div>&nbsp;</div>
<div>void setAutoRangeStickyZero(boolean flag)是否强制在整个数据轴中包含0，即使0不在数据范围中</div>
<div>&nbsp;</div>
<div>void setNumberFormatOverride(NumberFormat formatter)数据轴数据标签的显示格式</div>
<div>&nbsp;</div>
<div>void setTickUnit(NumberTickUnit unit)数据轴的数据标签（需要将AutoTickUnitSelection设false）</div>
<div>&nbsp;</div>
<div>DateAxis(ValueAxis)类：</div>
<div>&nbsp;</div>
<div>void setMaximumDate(Date maximumDate)日期轴上的最小日期</div>
<div>&nbsp;</div>
<div>void setMinimumDate(Date minimumDate)日期轴上的最大日期</div>
<div>&nbsp;</div>
<div>void setRange(Date lower,Date upper)日期轴范围</div>
<div>&nbsp;</div>
<div>void setDateFormatOverride(DateFormat formatter)日期轴日期标签的显示格式</div>
<div>&nbsp;</div>
<div>void setTickUnit(DateTickUnit unit)日期轴的日期标签（需要将AutoTickUnitSelection设false）</div>
<div>&nbsp;</div>
<div>void setTickMarkPosition(DateTickMarkPosition position)日期标签位置（参数常量在org.jfree.chart.axis.DateTickMarkPosition类中定义）</div>
<div>&nbsp;</div>
<div>CategoryAxis(Axis)类：</div>
<div>&nbsp;</div>
<div>void setCategoryMargin(double margin)分类轴边距</div>
<div>&nbsp;</div>
<div>void setLowerMargin(double margin)分类轴下（左）边距</div>
<div>&nbsp;</div>
<div>void setUpperMargin(double margin)分类轴上（右）边距</div>
<div>&nbsp;</div>
<div>void setVerticalCategoryLabels(boolean flag)分类轴标题是否旋转到垂直</div>
<div>&nbsp;</div>
<div>void setMaxCategoryLabelWidthRatio(float ratio)分类轴分类标签的最大宽度</div>
]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2010/12/jfreechart%e6%a0%b8%e5%bf%83%e7%b1%bb%e5%ba%93.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JFreeChart使用方法(代码备忘)</title>
		<link>http://fatkun.com/2010/12/jfreechart.html</link>
		<comments>http://fatkun.com/2010/12/jfreechart.html#comments</comments>
		<pubDate>Fri, 31 Dec 2010 05:50:31 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[jfreechart]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=761</guid>
		<description><![CDATA[以下文章介绍JFreeChart的使用方法。 文章来源：http://hi.baidu.com/baileyfu/blog/item/8e75b212b47fea54f819b808.html 一：jfreechart介绍 jfreechart是一个免费创建图片的java工具.可以创建如下图形： 饼图(pie charts;) 曲线图(line charts ) 柱状图(horizontal/vertical bar charts) 甘特图(Gantt charts; ) XY plots and scatter plots; time series, high/low/open/close charts and candle stick charts; combination charts; Pareto charts; bubble charts; wind plots, meter charts and symbol charts; 从以下地址可以看到jfreechart可以创建的图形类型 http://www.jfree.org/jfreechart/samples.html sourceforge有一个基于jfreechart的项目Cewolf可以很方便的在jsp/servlet中创建图片 jfreechart目前(2003-05-08)版本为0.98 希望得到详细的信息或下载jfreechart请访问如下站点: http://www.jfree.org/jfreechart/ 二：特别说明: jfreechart是一个开源项目，但是文档是需要40美金去购买的。 还有一个很重要的问题,jfreechart如果使用中文，他使用的默认字体 显示出来的中文会很模糊，你可能需要修改源代码。 下面我就举几个简单的例子说明一下如何使用jfreechart创建图片 在开发中有可能会导入以下的类 import com.jrefinery.chart.ChartFactory; [...]]]></description>
			<content:encoded><![CDATA[<p>以下文章介绍JFreeChart的使用方法。<br />
文章来源：http://hi.baidu.com/baileyfu/blog/item/8e75b212b47fea54f819b808.html<br />
<span id="more-761"></span></p>
<p>一：jfreechart介绍<br />
jfreechart是一个免费创建图片的java工具.可以创建如下图形：<br />
饼图(pie charts;)<br />
曲线图(line charts )<br />
柱状图(horizontal/vertical bar charts)<br />
甘特图(Gantt charts; )<br />
XY plots and scatter plots;<br />
time series, high/low/open/close charts and candle stick charts;<br />
combination charts;<br />
Pareto charts;<br />
bubble charts;<br />
wind plots, meter charts and symbol charts;<br />
从以下地址可以看到jfreechart可以创建的图形类型</p>
<p>http://www.jfree.org/jfreechart/samples.html</p>
<p>sourceforge有一个基于jfreechart的项目Cewolf可以很方便的在jsp/servlet中创建图片<br />
jfreechart目前(2003-05-08)版本为0.98<br />
希望得到详细的信息或下载jfreechart请访问如下站点:</p>
<p>http://www.jfree.org/jfreechart/</p>
<p>二：特别说明:<br />
jfreechart是一个开源项目，但是文档是需要40美金去购买的。<br />
还有一个很重要的问题,jfreechart如果使用中文，他使用的默认字体<br />
显示出来的中文会很模糊，你可能需要修改源代码。<br />
下面我就举几个简单的例子说明一下如何使用jfreechart创建图片<br />
在开发中有可能会导入以下的类<br />
import com.jrefinery.chart.ChartFactory;<br />
import com.jrefinery.chart.ChartUtilities;<br />
import com.jrefinery.chart.JFreeChart;<br />
import com.jrefinery.chart.TextTitle;<br />
import com.jrefinery.chart.axis.NumberAxis;<br />
import com.jrefinery.chart.plot.CategoryPlot;<br />
import com.jrefinery.chart.plot.PiePlot;<br />
import com.jrefinery.data.Day;<br />
import com.jrefinery.data.DefaultCategoryDataset;<br />
import com.jrefinery.data.DefaultPieDataset;<br />
import com.jrefinery.data.TimeSeries;<br />
import com.jrefinery.data.TimeSeriesCollection;<br />
import com.jrefinery.data.TimeSeriesDataPair;<br />
在0.98以后包由com.jrefinery.*改变为:org.jfree</p>
<p>三：创建饼图</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//图片标题</span>
<span style="color: #003399;">String</span> title <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;空调2002年市场占有率&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//设定数据源</span>
DefaultPieDataset piedata <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DefaultPieDataset<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//第一个参数为名称，第二个参数是double数</span>
piedata.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;联想&quot;</span>, <span style="color: #cc66cc;">27.3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
piedata.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;长城&quot;</span>, <span style="color: #cc66cc;">12.2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
piedata.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;海尔&quot;</span>, <span style="color: #cc66cc;">5.5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
piedata.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;美的&quot;</span>, <span style="color: #cc66cc;">17.1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
piedata.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;松下&quot;</span>, <span style="color: #cc66cc;">9.0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
piedata.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;科龙&quot;</span>, <span style="color: #cc66cc;">19.0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//创建JFreeChart，都使用ChartFactory来创建JFreeChart,很标准的工厂设计模式</span>
JFreeChart chart <span style="color: #339933;">=</span>
ChartFactory.<span style="color: #006633;">createPieChart</span><span style="color: #009900;">&#40;</span>title, piedata, <span style="color: #000066; font-weight: bold;">true</span>, <span style="color: #000066; font-weight: bold;">true</span>, <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//设定图片标题</span>
chart.<span style="color: #006633;">setTitle</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> TextTitle<span style="color: #009900;">&#40;</span>title, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Font</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;隶书&quot;</span>, <span style="color: #003399;">Font</span>.<span style="color: #006633;">ITALIC</span>, <span style="color: #cc66cc;">15</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//chart.addSubtitle(new TextTitle(&quot;2002财年分析&quot;, new Font(&quot;隶书&quot;, Font.ITALIC, 12)));</span>
<span style="color: #666666; font-style: italic;">//设定背景</span>
chart.<span style="color: #006633;">setBackgroundPaint</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Color</span>.<span style="color: #006633;">white</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//chart.s</span>
<span style="color: #666666; font-style: italic;">//饼图使用一个PiePlot </span>
PiePlot pie <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>PiePlot<span style="color: #009900;">&#41;</span>chart.<span style="color: #006633;">getPlot</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//pie.setSectionLabelType(PiePlot.NAME_AND_PERCENT_LABELS);</span>
pie.<span style="color: #006633;">setSectionLabelType</span><span style="color: #009900;">&#40;</span>PiePlot.<span style="color: #006633;">NAME_AND_VALUE_LABELS</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//设定显示格式(名称加百分比或数值)</span>
pie.<span style="color: #006633;">setPercentFormatString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#,###0.0#%&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//设定百分比显示格式</span>
pie.<span style="color: #006633;">setBackgroundPaint</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Color</span>.<span style="color: #006633;">white</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
pie.<span style="color: #006633;">setSectionLabelFont</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Font</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;黑体&quot;</span>, <span style="color: #003399;">Font</span>.<span style="color: #006633;">TRUETYPE_FONT</span>, <span style="color: #cc66cc;">12</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//设定背景透明度（0-1.0之间）</span>
pie.<span style="color: #006633;">setBackgroundAlpha</span><span style="color: #009900;">&#40;</span>0.6f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//设定前景透明度（0-1.0之间）</span>
pie.<span style="color: #006633;">setForegroundAlpha</span><span style="color: #009900;">&#40;</span>0.90f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//输出文件到指定目录</span>
<span style="color: #003399;">String</span> rfname <span style="color: #339933;">=</span> MathUtil.<span style="color: #006633;">getRoundCode</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">12</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;.jpeg&quot;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> fileName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;d:/test/&quot;</span> <span style="color: #339933;">+</span> rfname<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//可以保存文件为jpg或png格式。</span>
ChartUtilities.<span style="color: #006633;">saveChartAsJPEG</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span>fileName<span style="color: #009900;">&#41;</span>, <span style="color: #cc66cc;">100</span>, chart, <span style="color: #cc66cc;">600</span>, <span style="color: #cc66cc;">600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//第一个参数为文件名</span>
<span style="color: #666666; font-style: italic;">//第二个参数质量</span>
<span style="color: #666666; font-style: italic;">//第三个参数为哪个chart创建图片</span>
<span style="color: #666666; font-style: italic;">//第四个宽度</span>
<span style="color: #666666; font-style: italic;">//第五个高度</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> exz<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;....Cant't Create image File&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>其实使用JFreeChart创建图片很简单，不同的的图片类型区别在于设置数据集</p>
<p>四：创建曲线图</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// create a default chart based on some sample data...</span>
<span style="color: #666666; font-style: italic;">//曲线图标题</span>
<span style="color: #003399;">String</span> title <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;趋势分析&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//曲线图X轴提示</span>
<span style="color: #003399;">String</span> domain <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;月份走势&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//曲线图Y轴提示</span>
<span style="color: #003399;">String</span> range <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;应收余额&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//曲线图自标题</span>
<span style="color: #003399;">String</span> subtitleStr <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;2003财年分析&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//创建时间数据源</span>
<span style="color: #666666; font-style: italic;">//每一个TimeSeries在图上是一条曲线</span>
TimeSeries ca <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TimeSeries<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;用友&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1999</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">2005</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> mon <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> mon <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">12</span><span style="color: #339933;">;</span> mon<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//ca.add(new Month(mon + 1, i), new Double(500 + Math.random() * 100));</span>
<span style="color: #666666; font-style: italic;">//TimeSeriesDataPair是一个时间点的数值体现</span>
ca.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>
<span style="color: #000000; font-weight: bold;">new</span> TimeSeriesDataPair<span style="color: #009900;">&#40;</span>
<span style="color: #000000; font-weight: bold;">new</span> Day<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, mon <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span>, i<span style="color: #009900;">&#41;</span>,
<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Double</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">500</span> <span style="color: #339933;">+</span> <span style="color: #003399;">Math</span>.<span style="color: #006633;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
TimeSeries ibm <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TimeSeries<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;金碟&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1999</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">2005</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> mon <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> mon <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">12</span><span style="color: #339933;">;</span> mon<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//ibm.add(new Month(mon+1,i),new Double(400-Math.random()*100));</span>
ibm.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>
<span style="color: #000000; font-weight: bold;">new</span> TimeSeriesDataPair<span style="color: #009900;">&#40;</span>
<span style="color: #000000; font-weight: bold;">new</span> Day<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, mon <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span>, i<span style="color: #009900;">&#41;</span>,
<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Double</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">400</span> <span style="color: #339933;">-</span> <span style="color: #003399;">Math</span>.<span style="color: #006633;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
TimeSeries king <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TimeSeries<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;东软&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1999</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">2005</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> mon <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> mon <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">12</span><span style="color: #339933;">;</span> mon<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//ibm.add(new Month(mon+1,i),new Double(400-Math.random()*100));</span>
king.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>
<span style="color: #000000; font-weight: bold;">new</span> TimeSeriesDataPair<span style="color: #009900;">&#40;</span>
<span style="color: #000000; font-weight: bold;">new</span> Day<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, mon <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span>, i<span style="color: #009900;">&#41;</span>,
<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Double</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">300</span> <span style="color: #339933;">-</span> <span style="color: #003399;">Math</span>.<span style="color: #006633;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">//时间曲线数据集合</span>
TimeSeriesCollection dataset <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TimeSeriesCollection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
dataset.<span style="color: #006633;">addSeries</span><span style="color: #009900;">&#40;</span>ca<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
dataset.<span style="color: #006633;">addSeries</span><span style="color: #009900;">&#40;</span>ibm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
dataset.<span style="color: #006633;">addSeries</span><span style="color: #009900;">&#40;</span>king<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//dataset.addSeries(jpy);</span>
<span style="color: #666666; font-style: italic;">//dataset.addSeries(mav);</span>
<span style="color: #666666; font-style: italic;">//时间曲线元素</span>
JFreeChart chart <span style="color: #339933;">=</span>
ChartFactory.<span style="color: #006633;">createTimeSeriesChart</span><span style="color: #009900;">&#40;</span>
title,
domain,
range,
dataset,
<span style="color: #000066; font-weight: bold;">true</span>,
<span style="color: #000066; font-weight: bold;">true</span>,
<span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// then customise it a little...</span>
TextTitle subtitle <span style="color: #339933;">=</span>
<span style="color: #000000; font-weight: bold;">new</span> TextTitle<span style="color: #009900;">&#40;</span>subtitleStr, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Font</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;黑体&quot;</span>, <span style="color: #003399;">Font</span>.<span style="color: #006633;">BOLD</span>, <span style="color: #cc66cc;">12</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
chart.<span style="color: #006633;">addSubtitle</span><span style="color: #009900;">&#40;</span>subtitle<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
chart.<span style="color: #006633;">setTitle</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> TextTitle<span style="color: #009900;">&#40;</span>title, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Font</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;隶书&quot;</span>, <span style="color: #003399;">Font</span>.<span style="color: #006633;">ITALIC</span>, <span style="color: #cc66cc;">15</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//pie.setSeriesLabelFont(new Font(&quot;黑体&quot;, Font.BOLD, 15));</span>
chart.<span style="color: #006633;">setBackgroundPaint</span><span style="color: #009900;">&#40;</span>
<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">GradientPaint</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #003399;">Color</span>.<span style="color: #006633;">white</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1000</span>, <span style="color: #003399;">Color</span>.<span style="color: #006633;">blue</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//sysout</span>
<span style="color: #666666; font-style: italic;">//输出文件到指定目录</span>
<span style="color: #003399;">String</span> rfname <span style="color: #339933;">=</span> MathUtil.<span style="color: #006633;">getRoundCode</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">22</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;.jpeg&quot;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> fileName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;d:/test/&quot;</span> <span style="color: #339933;">+</span> rfname<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//for</span>
<span style="color: #666666; font-style: italic;">//System.out.println();</span>
ChartUtilities.<span style="color: #006633;">saveChartAsJPEG</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span>fileName<span style="color: #009900;">&#41;</span>, <span style="color: #cc66cc;">100</span>, chart, <span style="color: #cc66cc;">600</span>, <span style="color: #cc66cc;">600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// log.info(&quot;....Create image File:&quot; + fileName);</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> exz<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;....Cant't Create image File&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>五：创建柱状图</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> title <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;柱状图测试&quot;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> domain <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;单位比较&quot;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> range <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;数值&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//CategoryDataset data = DemoDatasetFactory.createCategoryDataset();</span>
DefaultCategoryDataset data <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DefaultCategoryDataset<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> r <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> r <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> r<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #003399;">String</span> rowKey <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;单位 [&quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>r <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;]&quot;</span> <span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//第一层循环：分析对象</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> c <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> c <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">;</span> c<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//第二层循环：分析对象在时间点上的数据</span>
<span style="color: #003399;">String</span> columnKey <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;2001年&quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>c <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;月&quot;</span><span style="color: #339933;">;</span>
data.<span style="color: #006633;">addValue</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Double</span><span style="color: #009900;">&#40;</span>r <span style="color: #339933;">*</span> c <span style="color: #339933;">+</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span>, rowKey, columnKey<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
JFreeChart chart <span style="color: #339933;">=</span>
ChartFactory.<span style="color: #006633;">createVerticalBarChart</span><span style="color: #009900;">&#40;</span>
title,
domain,
range,
data,
<span style="color: #000066; font-weight: bold;">true</span>,
<span style="color: #000066; font-weight: bold;">true</span>,
<span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// then customise it a little...</span>
chart.<span style="color: #006633;">setBackgroundPaint</span><span style="color: #009900;">&#40;</span>
<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">GradientPaint</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #003399;">Color</span>.<span style="color: #006633;">white</span>, <span style="color: #cc66cc;">1000</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #003399;">Color</span>.<span style="color: #006633;">red</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
chart.<span style="color: #006633;">setTitle</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> TextTitle<span style="color: #009900;">&#40;</span>title, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Font</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;隶书&quot;</span>, <span style="color: #003399;">Font</span>.<span style="color: #006633;">ITALIC</span>, <span style="color: #cc66cc;">15</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
CategoryPlot plot <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>CategoryPlot<span style="color: #009900;">&#41;</span>chart.<span style="color: #006633;">getPlot</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
plot.<span style="color: #006633;">setForegroundAlpha</span><span style="color: #009900;">&#40;</span>0.9f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
plot.<span style="color: #006633;">setValueLabelFont</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Font</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;黑体&quot;</span>, <span style="color: #003399;">Font</span>.<span style="color: #006633;">TRUETYPE_FONT</span>, <span style="color: #cc66cc;">12</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//plot.setSectionLabelFont(new Font(&quot;黑体&quot;, Font.TRUETYPE_FONT, 12));</span>
<span style="color: #666666; font-style: italic;">//注意以下代码</span>
NumberAxis verticalAxis <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>NumberAxis<span style="color: #009900;">&#41;</span>plot.<span style="color: #006633;">getRangeAxis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
verticalAxis.<span style="color: #006633;">setStandardTickUnits</span><span style="color: #009900;">&#40;</span>NumberAxis.<span style="color: #006633;">createIntegerTickUnits</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// 输出文件到指定目录</span>
<span style="color: #003399;">String</span> rfname <span style="color: #339933;">=</span> MathUtil.<span style="color: #006633;">getRoundCode</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">22</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;b.jpeg&quot;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> fileName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;d:/test/&quot;</span> <span style="color: #339933;">+</span> rfname<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
ChartUtilities.<span style="color: #006633;">saveChartAsJPEG</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span>fileName<span style="color: #009900;">&#41;</span>, <span style="color: #cc66cc;">100</span>, chart, <span style="color: #cc66cc;">600</span>, <span style="color: #cc66cc;">600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// log.info(&quot;....Create image File:&quot; + fileName);</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> exz<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;....Cant't Create image File&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>六：结束语<br />
个人感觉JFreeChart可以满足大部分图片创建的需要，美中不足的是：对字体的设置做的<br />
不 够好，特别是使用中文的时候字体很不清晰。因为这个原因建议你自己去修改他的源代码,最好使用properties文件去设置字体.还有就是文档要钱所以 要多花点时间去看源代码。或多上社区.因为时间等原因我只介绍了三种图片的创建，其他类型的图片可以参考jfreechart提供的例子。</p>
]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2010/12/jfreechart.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>hibernate事务管理</title>
		<link>http://fatkun.com/2010/12/hibernate-transaction.html</link>
		<comments>http://fatkun.com/2010/12/hibernate-transaction.html#comments</comments>
		<pubDate>Sat, 25 Dec 2010 10:16:03 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[数据库]]></category>
		<category><![CDATA[数据库事务]]></category>
		<category><![CDATA[隔离级别]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=722</guid>
		<description><![CDATA[1. 介绍数据库事务、事务隔离级别、悲观锁、乐观锁等概念。 2.数据库ACID特征： Atomic（原子性）：指整个数据库事务是不可分割的工作单元。 Consistency（一致性）：指数据库事务不能破坏关系数据的完整性以及业务逻辑上的一致性。 Isolation（隔离性）：指的是在并发环境中，当不同的事务同时操纵相同的数据时，每个事务都有各自的完整数据空间。 Durability（持久性）：指的是只要事务成功结束，它对数据库所作的更新就必须永久保存下来。 3.数据库系统支持两种事务模式： 自动提交模式：每个SQL语句都是一个独立的事务，当数据库系统执行完一个SQL语句后，会自动提交事务。 手动提交模式：必须由数据库客户程序显示指定事务开始边界和结束边界。 4.MySQL中数据库表分为3种类型： INNODB、BDB和MyISAM，其中MyISAM不支持数据库事务。MySQL中create table 语句默认为MyISAM类型。 5.并发问题 对于同时运行的多个事务，当这些事务访问数据库中相同的数据时，如果没有采取必要的隔离机制，就会导致各种并发问题，这些并发问题可归纳为以下几类： A.第一类丢失更新：撤销一个事务时，把其他事务已提交的更新数据覆盖。 B.脏读：一个事务读到另一个事务为提交的更新数据。 C.幻读：一个事务读到另一个事务已提交的新插入的数据。 D.不可重复读：一个事务读到另一个事务已提交的更新数据。 E.第二类丢失更新：这是不可重复读中的特例，一个事务覆盖另一个事务已提交的更新数据。 6.数据库系统四种事务隔离级别 A.Serializable（串行化）：一个事务在执行过程中完全看不到其他事务对数据库所做的更新。 B.Repeatable Read（可重复读）：一个事务在执行过程中可以看到其他事务已经提交的新插入的记录，但是不能看到其他其他事务对已有记录的更新。 C.Read Commited（读已提交数据）：一个事务在执行过程中可以看到其他事务已经提交的新插入的记录，而且能看到其他事务已经提交的对已有记录的更新。 D.Read Uncommitted（读未提交数据）：一个事务在执行过程中可以拷打其他事务没有提交的新插入的记录，而且能看到其他事务没有提交的对已有记录的更新。 隔离级别 脏读 不可 重复读 幻象读 第一类丢失更新 第二类丢失更新 READ UNCOMMITED 允许 允许 允许 不允许 允许 READ COMMITTED 不允许 允许 允许 不允许 允许 REPEATABLE READ 不允许 不允许 允许 不允许 [...]]]></description>
			<content:encoded><![CDATA[<h2>1. 介绍数据库事务、事务隔离级别、悲观锁、乐观锁等概念。</h2>
<h2>2.数据库ACID特征：</h2>
<blockquote><p>Atomic（原子性）：指整个数据库事务是不可分割的工作单元。</p>
<p>Consistency（一致性）：指数据库事务不能破坏关系数据的完整性以及业务逻辑上的一致性。</p>
<p>Isolation（隔离性）：指的是在并发环境中，当不同的事务同时操纵相同的数据时，每个事务都有各自的完整数据空间。</p>
<p>Durability（持久性）：指的是只要事务成功结束，它对数据库所作的更新就必须永久保存下来。</p></blockquote>
<h2>3.数据库系统支持两种事务模式：</h2>
<p style="padding-left: 30px;">自动提交模式：每个SQL语句都是一个独立的事务，当数据库系统执行完一个SQL语句后，会自动提交事务。</p>
<p style="padding-left: 30px;">手动提交模式：必须由数据库客户程序显示指定事务开始边界和结束边界。</p>
<h2>4.MySQL中数据库表分为3种类型：</h2>
<p style="padding-left: 30px;">INNODB、BDB和MyISAM，其中MyISAM不支持数据库事务。MySQL中create table 语句默认为MyISAM类型。</p>
<h2>5.并发问题</h2>
<p>对于同时运行的多个事务，当这些事务访问数据库中相同的数据时，如果没有采取必要的隔离机制，就会导致各种并发问题，这些并发问题可归纳为以下几类：</p>
<blockquote><p>A.第一类丢失更新：撤销一个事务时，把其他事务已提交的更新数据覆盖。</p>
<p>B.脏读：一个事务读到另一个事务为提交的更新数据。</p>
<p>C.幻读：一个事务读到另一个事务已提交的新插入的数据。</p>
<p>D.不可重复读：一个事务读到另一个事务已提交的更新数据。</p>
<p>E.第二类丢失更新：这是不可重复读中的特例，一个事务覆盖另一个事务已提交的更新数据。</p></blockquote>
<h2>6.数据库系统四种事务隔离级别</h2>
<p style="padding-left: 30px;">A.Serializable（串行化）：一个事务在执行过程中完全看不到其他事务对数据库所做的更新。</p>
<p style="padding-left: 30px;">B.Repeatable Read（可重复读）：一个事务在执行过程中可以看到其他事务已经提交的新插入的记录，但是不能看到其他其他事务对已有记录的更新。</p>
<p style="padding-left: 30px;">C.Read Commited（读已提交数据）：一个事务在执行过程中可以看到其他事务已经提交的新插入的记录，而且能看到其他事务已经提交的对已有记录的更新。</p>
<p style="padding-left: 30px;">D.Read Uncommitted（读未提交数据）：一个事务在执行过程中可以拷打其他事务没有提交的新插入的记录，而且能看到其他事务没有提交的对已有记录的更新。</p>
<table border="1" cellspacing="0" cellpadding="0" width="576">
<tbody>
<tr>
<td width="164">
<div>隔离级别</div>
</td>
<td width="82">
<div>脏读</div>
</td>
<td width="82">
<div>不可</div>
<div>重复读</div>
</td>
<td width="82">
<div>幻象读</div>
</td>
<td width="82">
<div>第一类丢失更新</div>
</td>
<td width="82">
<div>第二类丢失更新</div>
</td>
</tr>
<tr>
<td width="164" valign="top">
<div>READ UNCOMMITED</div>
</td>
<td width="82">
<div>允许</div>
</td>
<td width="82">
<div>允许</div>
</td>
<td width="82">
<div>允许</div>
</td>
<td width="82">
<div>不允许</div>
</td>
<td width="82">
<div>允许</div>
</td>
</tr>
<tr>
<td width="164" valign="top">
<div>READ COMMITTED</div>
</td>
<td width="82">
<div>不允许</div>
</td>
<td width="82">
<div>允许</div>
</td>
<td width="82">
<div>允许</div>
</td>
<td width="82">
<div>不允许</div>
</td>
<td width="82">
<div>允许</div>
</td>
</tr>
<tr>
<td width="164" valign="top">
<div>REPEATABLE READ</div>
</td>
<td width="82">
<div>不允许</div>
</td>
<td width="82">
<div>不允许</div>
</td>
<td width="82">
<div>允许</div>
</td>
<td width="82">
<div>不允许</div>
</td>
<td width="82">
<div>不允许</div>
</td>
</tr>
<tr>
<td width="164" valign="top">
<div>SERIALIZABLE</div>
</td>
<td width="82">
<div>不允许</div>
</td>
<td width="82">
<div>不允许</div>
</td>
<td width="82">
<div>不允许</div>
</td>
<td width="82">
<div>不允许</div>
</td>
<td width="82">
<div>不允许</div>
</td>
</tr>
</tbody>
</table>
<p style="padding-left: 30px;">隔离级别越高，越能保证数据的完整性和一致性，但是对并发性能的影响也越大。对于多数应用程序，可以有优先考虑把数据库系统的隔离级别设为Read Commited，它能够避免脏读，而且具有较好的并发性能。尽管它会导致不可重复读、幻读和第二类丢失更新这些并发问题，在可能出现这类问题的个别场合，可以由应用程序采用悲观锁或乐观锁来控制。</p>
<h2>7.当数据库系统采用read Commited隔离级别时</h2>
<p>会导致不可重复读喝第二类丢失更新的并发问题，可以在应用程序中采用悲观锁或乐观锁来避免这类问题。从应用程序的角度，锁可以分为以下几类：</p>
<p style="padding-left: 30px;">A.悲观锁：指在应用程序中显示的为数据资源加锁。尽管能防止丢失更新和不可重复读这类并发问题，但是它会影响并发性能，因此应该谨慎地使用。</p>
<p style="padding-left: 30px;">B.乐观锁：乐观锁假定当前事务操作数据资源时，不回有其他事务同时访问该数据资源，因此完全依靠数据库的隔离级别来自动管理锁的工作。应用程序采用版本控制手段来避免可能出现的并发问题。</p>
<h2>8.悲观锁有两种实现方式：</h2>
<p style="padding-left: 30px;">A.在应用程序中显示指定采用数据库系统的独占所来锁定数据资源。SQL语句：select &#8230; for update，在Hibernate中使用get，load时如session.get(Account.class,new Long(1),LockMode,UPGRADE)</p>
<p style="padding-left: 30px;">B.在数据库表中增加一个表明记录状态的LOCK字段，当它取值为“Y”时，表示该记录已经被某个事务锁定，如果为“N”，表明该记录处于空闲状态，事务可以访问它。增加锁标记字段就可以实现。</p>
<h2>9.利用Hibernate的版本控制来实现乐观锁</h2>
<p style="padding-left: 30px;">乐观锁是由程序提供的一种机制，这种机制既能保证多个事务并发访问数据，又能防止第二类丢失更新问题。</p>
<p style="padding-left: 30px;">在应用程序中可以利用Hibernate提供的版本控制功能来视线乐观锁，OR映射文件中的&lt;version&gt;元素和&lt;timestamp&gt;都具有版本控制的功能，一般推荐采用&lt;version&gt;</p>
<p>本文来源：<a href="http://blog.csdn.net/yueguangyuan/archive/2006/09/20/1253229.aspx">http://blog.csdn.net/yueguangyuan/archive/2006/09/20/1253229.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2010/12/hibernate-transaction.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>List中的remove使用注意</title>
		<link>http://fatkun.com/2010/12/list-remove.html</link>
		<comments>http://fatkun.com/2010/12/list-remove.html#comments</comments>
		<pubDate>Tue, 21 Dec 2010 05:09:50 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[java基础]]></category>
		<category><![CDATA[list]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=717</guid>
		<description><![CDATA[先来看API是怎样写的。 boolean remove(Object o) 从此列表中移除第一次出现的指定元素（如果存在）（可选操作）。如果列表不包含元素，则不更改列表。更确切地讲，移除满足 (o==null ? get(i)==null : o.equals(get(i))) 的最低索引 i 的元素（如果存在这样的元素）。如果此列表已包含指定元素（或者此列表由于调用而发生更改），则返回 true。 可以看到是用o.equals来判断是否删除的，在一般情况下使用都没有问题。但我有一次犯了一个错误。 有一个List&#60;子类&#62; list，我用 list.remve(父类对象);这样很明显是不行的，equals不了，所以就删除不了。 解决方法：1，是remove同一类对象 2，在父类覆盖equals方法，用instanceof来判断是不是子类，然后判断相等。 方法2较为麻烦。还是建议方法1。]]></description>
			<content:encoded><![CDATA[<p>先来看API是怎样写的。</p>
<blockquote><p>boolean <strong>remove</strong>(<a title="java.lang 中的类">Object</a> o)</p>
<dl>
<dd>从此列表中移除第一次出现的指定元素（如果存在）（可选操作）。如果列表不包含元素，则不更改列表。更确切地讲，移除满足  <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt> 的最低索引 <tt>i</tt> 的元素（如果存在这样的元素）。如果此列表已包含指定元素（或者此列表由于调用而发生更改），则返回 <tt>true</tt>。 </dd>
</dl>
</blockquote>
<p>可以看到是用o.equals来判断是否删除的，在一般情况下使用都没有问题。但我有一次犯了一个错误。</p>
<p>有一个List&lt;子类&gt; list，我用 list.remve(父类对象);这样很明显是不行的，equals不了，所以就删除不了。</p>
<p>解决方法：1，是remove同一类对象</p>
<p>2，在父类覆盖equals方法，用instanceof来判断是不是子类，然后判断相等。</p>
<p>方法2较为麻烦。还是建议方法1。</p>
]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2010/12/list-remove.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>选择排序算法(JAVA版)</title>
		<link>http://fatkun.com/2010/12/select-sort-java.html</link>
		<comments>http://fatkun.com/2010/12/select-sort-java.html#comments</comments>
		<pubDate>Sat, 04 Dec 2010 15:24:59 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[排序算法]]></category>
		<category><![CDATA[数据结构]]></category>
		<category><![CDATA[算法]]></category>
		<category><![CDATA[选择排序]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=713</guid>
		<description><![CDATA[选择排序和插入排序差不多，交换的次数减少。平均/最好/最坏时间复杂度是O(n2)，是不稳定的排序算法。（插入排序是稳定排序算法） 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 /* * 选择排序：选择最小/大的放在前面，每一趟前几个按序排列 */ int&#91;&#93; selectSort&#40;int&#91;&#93; arr&#41; &#123; for &#40;int i = 0; i &#60; arr.length - 1; i++&#41; &#123; int k = i; //和i后面的每一个比较，如果有比i小的记下下标k for &#40;int j = i + 1; j &#60; arr.length; [...]]]></description>
			<content:encoded><![CDATA[<p>选择排序和插入排序差不多，交换的次数减少。平均/最好/最坏时间复杂度是O(n<sup>2</sup>)，是不稳定的排序算法。（插入排序是稳定排序算法）</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">	<span style="color: #666666; font-style: italic;">/*
	 * 选择排序：选择最小/大的放在前面，每一趟前几个按序排列
	 */</span>
	<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> selectSort<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> arr<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> arr.<span style="color: #006633;">length</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">int</span> k <span style="color: #339933;">=</span> i<span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">//和i后面的每一个比较，如果有比i小的记下下标k</span>
			<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> i <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> arr.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> arr<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
					k <span style="color: #339933;">=</span> j<span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>k <span style="color: #339933;">!=</span> i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #666666; font-style: italic;">//交换</span>
				<span style="color: #000066; font-weight: bold;">int</span> temp <span style="color: #339933;">=</span> arr<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				arr<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> arr<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				arr<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> temp<span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> arr<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2010/12/select-sort-java.html/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>快速排序算法(JAVA版)</title>
		<link>http://fatkun.com/2010/12/quick-sort-java.html</link>
		<comments>http://fatkun.com/2010/12/quick-sort-java.html#comments</comments>
		<pubDate>Sat, 04 Dec 2010 02:57:47 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[快速排序]]></category>
		<category><![CDATA[排序算法]]></category>
		<category><![CDATA[算法]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=708</guid>
		<description><![CDATA[快速排序是不稳定的排序方法，平均时间复杂度为O(nlogn)，空间复杂度为O(logn)，最差时是有序或基本有序时，算法退化为冒泡排序，时间复杂度是O(n2) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 /* * 快速排序：选一个值pivokey(一般是第一个),小于pivokey放在左边，大于pivokey放在右边，分成两个序列，递归直到low &#62;= high * 步骤： * 1，设定两个指针low,high。初值是0和数组长度-1，指定关键字pivokey * 2，从high所指的位置向前找，找到一个比pivokey小的，和关键字交换值(arr[low] = arr[high]) * 3，从low所指的位置向后找，找到一个比pivokey大的，和关键字交换值 * 4，直到low&#62;=high，把关键字赋值arr[low]=pivokey [...]]]></description>
			<content:encoded><![CDATA[<p>快速排序是不稳定的排序方法，平均时间复杂度为O(nlogn)，空间复杂度为O(logn)，最差时是有序或基本有序时，算法退化为冒泡排序，时间复杂度是O(n<sup>2</sup>)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">	<span style="color: #666666; font-style: italic;">/*
	 * 快速排序：选一个值pivokey(一般是第一个),小于pivokey放在左边，大于pivokey放在右边，分成两个序列，递归直到low &gt;= high
	 * 步骤：
	 * 1，设定两个指针low,high。初值是0和数组长度-1，指定关键字pivokey
	 * 2，从high所指的位置向前找，找到一个比pivokey小的，和关键字交换值(arr[low] = arr[high])
	 * 3，从low所指的位置向后找，找到一个比pivokey大的，和关键字交换值
	 * 4，直到low&gt;=high，把关键字赋值arr[low]=pivokey
	 * 5，递归分出的两个序列[lowStart,low-1]  [low+1,highEnd]
	 * */</span>
	<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> quickSort<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> arr, <span style="color: #000066; font-weight: bold;">int</span> low, <span style="color: #000066; font-weight: bold;">int</span> high<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//为后面的递归使用</span>
		<span style="color: #000066; font-weight: bold;">int</span> lowStart <span style="color: #339933;">=</span> low<span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> highEnd <span style="color: #339933;">=</span> high<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>low <span style="color: #339933;">&lt;</span> high<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">int</span> pivokey <span style="color: #339933;">=</span> arr<span style="color: #009900;">&#91;</span>low<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>low <span style="color: #339933;">&lt;</span> high<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">//如果都是大于pivokey，high指针往前移</span>
				<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>low <span style="color: #339933;">&lt;</span> high <span style="color: #339933;">&amp;&amp;</span> arr<span style="color: #009900;">&#91;</span>high<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> pivokey<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					high<span style="color: #339933;">--;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #666666; font-style: italic;">//这里low++是先把arr[low]赋值为arr[high]，再low+=1，因为这个值是比pivokey小的，下一次不用比较了</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>low <span style="color: #339933;">&lt;</span> high<span style="color: #009900;">&#41;</span>
				arr<span style="color: #009900;">&#91;</span>low<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> arr<span style="color: #009900;">&#91;</span>high<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #666666; font-style: italic;">//如果都是小于pivokey，low指针往后移</span>
				<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>low <span style="color: #339933;">&lt;</span> high <span style="color: #339933;">&amp;&amp;</span> arr<span style="color: #009900;">&#91;</span>low<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> pivokey<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					low<span style="color: #339933;">++;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #666666; font-style: italic;">//这里的high--和上面同理</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>low <span style="color: #339933;">&lt;</span> high<span style="color: #009900;">&#41;</span>
				arr<span style="color: #009900;">&#91;</span>high<span style="color: #339933;">--</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> arr<span style="color: #009900;">&#91;</span>low<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			arr<span style="color: #009900;">&#91;</span>low<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> pivokey<span style="color: #339933;">;</span>
			quickSort<span style="color: #009900;">&#40;</span>arr, lowStart, low <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			quickSort<span style="color: #009900;">&#40;</span>arr, low <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span>, highEnd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> arr<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2010/12/quick-sort-java.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>冒泡排序算法(JAVA)</title>
		<link>http://fatkun.com/2010/12/bubble-sort.html</link>
		<comments>http://fatkun.com/2010/12/bubble-sort.html#comments</comments>
		<pubDate>Fri, 03 Dec 2010 16:00:18 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[冒泡排序]]></category>
		<category><![CDATA[排序算法]]></category>
		<category><![CDATA[算法]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=703</guid>
		<description><![CDATA[冒泡排序：和每一个比较，如果比后面大则交换，最终每一趟结果是最大值会沉到后面。 时间复杂度O(n2)，最佳情况是已排好序只比较n-1次，不用交换。 int&#91;&#93; bubbleSort&#40;int&#91;&#93; a&#41; &#123; //每个都进行冒泡(一个一个来) for &#40;int i = 0; i &#60; a.length; i++&#41; &#123; &#160; //和前n-i个比较，把最大的数沉下去 int temp; for &#40;int j = 0; j &#60; a.length - i - 1; j++&#41; &#123; if &#40;a&#91;j&#93; &#62; a&#91;j + 1&#93;&#41; &#123; //交换 temp = a&#91;j&#93;; a&#91;j&#93; = a&#91;j + 1&#93;; a&#91;j + 1&#93; = [...]]]></description>
			<content:encoded><![CDATA[<p>冒泡排序：和每一个比较，如果比后面大则交换，最终每一趟结果是最大值会沉到后面。<br />
时间复杂度O(n<sup>2</sup>)，最佳情况是已排好序只比较n-1次，不用交换。</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> bubbleSort<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> a<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//每个都进行冒泡(一个一个来)</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> a.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">//和前n-i个比较，把最大的数沉下去</span>
			<span style="color: #000066; font-weight: bold;">int</span> temp<span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> a.<span style="color: #006633;">length</span> <span style="color: #339933;">-</span> i <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> a<span style="color: #009900;">&#91;</span>j <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #666666; font-style: italic;">//交换</span>
					temp <span style="color: #339933;">=</span> a<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
					a<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> a<span style="color: #009900;">&#91;</span>j <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
					a<span style="color: #009900;">&#91;</span>j <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> temp<span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> a<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2010/12/bubble-sort.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

