﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>语源科技BlogJava-不逼自己一把，永远不知道自己多优秀</title><link>http://www.blogjava.net/dongisland/</link><description>把握每一天的精彩</description><language>zh-cn</language><lastBuildDate>Sun, 12 Apr 2026 17:25:35 GMT</lastBuildDate><pubDate>Sun, 12 Apr 2026 17:25:35 GMT</pubDate><ttl>60</ttl><item><title>java求连续子数组的和最大</title><link>http://www.blogjava.net/dongisland/archive/2014/03/05/410637.html</link><dc:creator>dongisland</dc:creator><author>dongisland</author><pubDate>Wed, 05 Mar 2014 03:47:00 GMT</pubDate><guid>http://www.blogjava.net/dongisland/archive/2014/03/05/410637.html</guid><wfw:comment>http://www.blogjava.net/dongisland/comments/410637.html</wfw:comment><comments>http://www.blogjava.net/dongisland/archive/2014/03/05/410637.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/dongisland/comments/commentRss/410637.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/dongisland/services/trackbacks/410637.html</trackback:ping><description><![CDATA[<p style="margin: 1.12em 0px; padding: 0px; color: #555555; font-family: Helvetica, Arial, sans-serif, 微软雅黑; font-size: small; line-height: 18.850000381469727px;"><strong style="margin: 0px; padding: 0px;">题目描述：</strong><br style="margin: 0px; padding: 0px;" />输入一个整形数组，数组里有正数也有负数。数组中连续的一个或多个整数组成一个子数组，每个子数组都有一个和。</p><p style="margin: 1.12em 0px; padding: 0px; color: #555555; font-family: Helvetica, Arial, sans-serif, 微软雅黑; font-size: small; line-height: 18.850000381469727px;">求所有子数组的和的最大值。要求时间复杂度为O(n)。</p><p style="margin: 1.12em 0px; padding: 0px; color: #555555; font-family: Helvetica, Arial, sans-serif, 微软雅黑; font-size: small; line-height: 18.850000381469727px;"><strong style="margin: 0px; padding: 0px;">思路：</strong></p><p style="margin: 1.12em 0px; padding: 0px; color: #555555; font-family: Helvetica, Arial, sans-serif, 微软雅黑; font-size: small; line-height: 18.850000381469727px;">我们可以使用分治法或者减治法来处理这个问题。</p><p style="margin: 1.12em 0px; padding: 0px; color: #555555; font-family: Helvetica, Arial, sans-serif, 微软雅黑; font-size: small; line-height: 18.850000381469727px;"><strong style="margin: 0px; padding: 0px;">分治法 &nbsp; &nbsp;<br /></strong><span style="font-style: italic;">目标：把1个大问题分成2个小问题，2个小问题还可以再分，直到问题规模小的可以简单解决。<br /></span></p><p style="margin: 1.12em 0px; padding: 0px; color: #555555; font-family: Helvetica, Arial, sans-serif, 微软雅黑; font-size: small; font-style: italic; line-height: 18.850000381469727px;">将该数组等分成两个子数组，假如知道左右两侧两个数组的各自的最大子数组和，那么整个数组的最大子数组和可能为三种情况：</p><ul style="margin: 1.12em 0px 1.12em 40px; padding: 0px; color: #555555; font-family: Helvetica, Arial, sans-serif, 微软雅黑; font-size: small; font-style: italic; line-height: 18.850000381469727px;"><li style="margin: 0px 0px 0px 12px; padding: 0px;">右侧数组的最大子数组和</li><li style="margin: 0px 0px 0px 12px; padding: 0px;">左侧数组的最大子数组和</li><li style="margin: 0px 0px 0px 12px; padding: 0px;">左右两侧数组中间毗邻位置连接形成的子数组的和的最大值（如-6，4，-3，2####5，-6，9，-8，左侧最大值为4，右侧为9，中间毗邻位置从2和5向两侧相加，得到中间毗邻子数组4，-3，2，5，-6，9和为11，三者比较得最大子数组和为11）。</li></ul><p style="margin: 1.12em 0px; padding: 0px; color: #555555; font-family: Helvetica, Arial, sans-serif, 微软雅黑; font-size: small; font-style: italic; line-height: 18.850000381469727px;">递归到数组中只包含一个数字。</p><p style="margin: 1.12em 0px; padding: 0px; color: #555555; font-family: Helvetica, Arial, sans-serif, 微软雅黑; font-size: small; font-style: italic; line-height: 18.850000381469727px;">这种思路也是可行的。进行ln(n)次拆分，每次拆分后进行n次比较，所以算法复杂度为n*ln(n)。但还达不到题目的要求。</p><p style="margin: 1.12em 0px; padding: 0px; color: #555555; font-family: Helvetica, Arial, sans-serif, 微软雅黑; font-size: small; font-style: italic; line-height: 18.850000381469727px;">java代码：<br /></p><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080; ">&nbsp;1</span>&nbsp;<span style="color: #0000FF; ">package</span>&nbsp;com.island.info;<br /><span style="color: #008080; ">&nbsp;2</span>&nbsp;<span style="color: #008000; ">/**</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">&nbsp;3</span>&nbsp;<span style="color: #008000; ">&nbsp;*&nbsp;&lt;p&gt;Title:&nbsp;TestMaxArray.java&lt;/p&gt;<br /></span><span style="color: #008080; ">&nbsp;4</span>&nbsp;<span style="color: #008000; ">&nbsp;*&nbsp;&lt;p&gt;Description:&nbsp;分治法求解连续和最大&lt;/p&gt;<br /></span><span style="color: #008080; ">&nbsp;5</span>&nbsp;<span style="color: #008000; ">&nbsp;*&nbsp;@date&nbsp;2014-3-05<br /></span><span style="color: #008080; ">&nbsp;6</span>&nbsp;<span style="color: #008000; ">&nbsp;*<br /></span><span style="color: #008080; ">&nbsp;7</span>&nbsp;<span style="color: #008000; ">&nbsp;</span><span style="color: #008000; ">*/</span><br /><span style="color: #008080; ">&nbsp;8</span>&nbsp;&nbsp;<br /><span style="color: #008080; ">&nbsp;9</span>&nbsp;<span style="color: #0000FF; ">public</span>&nbsp;<span style="color: #0000FF; ">class</span>&nbsp;MaxSub&nbsp;{<br /><span style="color: #008080; ">10</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">static</span>&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;arr[]&nbsp;=&nbsp;{4,-3,5,-2,-1,2,6,-2};&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">也可以随机生成</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">11</span>&nbsp;<span style="color: #008000; "></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">public</span>&nbsp;<span style="color: #0000FF; ">static</span>&nbsp;<span style="color: #0000FF; ">void</span>&nbsp;main(String&nbsp;args[]){<br /><span style="color: #008080; ">12</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(max(arr));<br /><span style="color: #008080; ">13</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><span style="color: #008080; ">14</span>&nbsp;&nbsp;<br /><span style="color: #008080; ">15</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">包装函数</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">16</span>&nbsp;<span style="color: #008000; "></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">public</span>&nbsp;<span style="color: #0000FF; ">static</span>&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;max(<span style="color: #0000FF; ">final</span>&nbsp;<span style="color: #0000FF; ">int</span>[]&nbsp;arr){<br /><span style="color: #008080; ">17</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("(1)*****arr.length-1-----------------&gt;:"+&nbsp;(arr.length-1));<br /><span style="color: #008080; ">18</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;max(arr,0,arr.length-1);<br /><span style="color: #008080; ">19</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><span style="color: #008080; ">20</span>&nbsp;&nbsp;<br /><span style="color: #008080; ">21</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">核心代码：递归调用max()</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">22</span>&nbsp;<span style="color: #008000; "></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">public</span>&nbsp;<span style="color: #0000FF; ">static</span>&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;max(<span style="color: #0000FF; ">final</span>&nbsp;<span style="color: #0000FF; ">int</span>[]&nbsp;arr,<span style="color: #0000FF; ">int</span>&nbsp;leftIndex,&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;rightIndex){<br /><span style="color: #008080; ">23</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("(2)*****leftIndex--------rightIndex---&gt;:"+leftIndex+"|***************|"+rightIndex);<br /><span style="color: #008080; ">24</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;sum&nbsp;=&nbsp;0,leftHalfMax&nbsp;=&nbsp;0,&nbsp;rightHalfMax&nbsp;=&nbsp;0;<br /><span style="color: #008080; ">25</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(rightIndex-leftIndex==0){<br /><span style="color: #008080; ">26</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;arr[rightIndex];<br /><span style="color: #008080; ">27</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<span style="color: #0000FF; ">else</span>&nbsp;{<br /><span style="color: #008080; ">28</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;center&nbsp;=&nbsp;(leftIndex+rightIndex)/2;<span style="color: #008000; ">//</span><span style="color: #008000; ">2分查找中间节点</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">29</span>&nbsp;<span style="color: #008000; "></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;maxLeft&nbsp;=&nbsp;max(arr,leftIndex,center);<span style="color: #008000; ">//</span><span style="color: #008000; ">左边最大的</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">30</span>&nbsp;<span style="color: #008000; "></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;maxRight&nbsp;=&nbsp;max(arr,center+1,rightIndex);<span style="color: #008000; ">//</span><span style="color: #008000; ">右边最大的<br /></span><span style="color: #008080; ">31</span>&nbsp;<span style="color: #008000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">以下是查找跨越中间点的最大子序列<br /></span><span style="color: #008080; ">32</span>&nbsp;<span style="color: #008000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">从中点到左侧：</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">33</span>&nbsp;<span style="color: #008000; "></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>&nbsp;(<span style="color: #0000FF; ">int</span>&nbsp;i=center;i&gt;=leftIndex;--i){<br /><span style="color: #008080; ">34</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum+=arr[i];<br /><span style="color: #008080; ">35</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(sum&gt;leftHalfMax){<br /><span style="color: #008080; ">36</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;leftHalfMax&nbsp;=&nbsp;sum;<br /><span style="color: #008080; ">37</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><span style="color: #008080; ">38</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><span style="color: #008080; ">39</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("左边的sum-----------&gt;:"+sum);<br /><span style="color: #008080; ">40</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum=0;<br /><span style="color: #008080; ">41</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">从中点到右侧</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">42</span>&nbsp;<span style="color: #008000; "></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>&nbsp;(<span style="color: #0000FF; ">int</span>&nbsp;i=center+1;i&lt;=rightIndex;++i){<br /><span style="color: #008080; ">43</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum+=arr[i];<br /><span style="color: #008080; ">44</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(sum&gt;rightHalfMax){<br /><span style="color: #008080; ">45</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rightHalfMax&nbsp;=&nbsp;sum;<br /><span style="color: #008080; ">46</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><span style="color: #008080; ">47</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><span style="color: #008080; ">48</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("右边的sum-----------&gt;:"+sum);<br /><span style="color: #008080; ">49</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;max(maxLeft,maxRight,leftHalfMax+rightHalfMax);<br /><span style="color: #008080; ">50</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><span style="color: #008080; ">51</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><span style="color: #008080; ">52</span>&nbsp;&nbsp;<br /><span style="color: #008080; ">53</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">三者取最大值</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">54</span>&nbsp;<span style="color: #008000; "></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">public</span>&nbsp;<span style="color: #0000FF; ">static</span>&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;max(<span style="color: #0000FF; ">int</span>&nbsp;a,<span style="color: #0000FF; ">int</span>&nbsp;b,<span style="color: #0000FF; ">int</span>&nbsp;c){<br /><span style="color: #008080; ">55</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;a&gt;b?(a&gt;c?a:c):(b&gt;c?b:c);<br /><span style="color: #008080; ">56</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><span style="color: #008080; ">57</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</div><p style="margin: 1.12em 0px; padding: 0px; color: #555555; font-family: Helvetica, Arial, sans-serif, 微软雅黑; font-size: small; line-height: 18.850000381469727px;"><strong style="margin: 0px; padding: 0px;">减治法</strong></p><div><strong style="margin: 0px; padding: 0px; color: #555555; font-family: Helvetica, Arial, sans-serif, 微软雅黑; font-size: small; line-height: 18.850000381469727px;"><p style="margin: 1.12em 0px; padding: 0px; font-style: italic; font-weight: normal;">目标：将问题规模不断减小，直到可以简单处理为止。</p><p style="margin: 1.12em 0px; padding: 0px; font-style: italic; font-weight: normal;">假设我们已知一个数组的最大子数组和，现在在该数组后面增加一个数字，新数组的最大子数组和可能是什么呢：</p><ul style="margin: 1.12em 0px 1.12em 40px; padding: 0px; font-style: italic; font-weight: normal;"><li style="margin: 0px 0px 0px 12px; padding: 0px;">原数组的最大子数组和；</li><li style="margin: 0px 0px 0px 12px; padding: 0px;">新增加的数字为正数，和最右侧的子数组形成了一个新的最大子数组和（所以为了通过一次遍历完成，我们需要保存最右侧的子数组的最大和）。</li></ul><p style="margin: 1.12em 0px; padding: 0px; font-style: italic; font-weight: normal;">然后将两个数字进行比较即可。</p><p style="margin: 1.12em 0px; padding: 0px; font-style: italic; font-weight: normal;">所以减治至数组只包含最左侧一个数字，我们知道它的最大子数组和和最右侧子数组最大和都为还数字，逐次加1个数字直到整个数组即可。</p><p style="margin: 1.12em 0px; padding: 0px; font-style: italic; font-weight: normal;">java代码：<br /></p><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080; ">&nbsp;1</span>&nbsp;<span style="color: #0000FF; ">package</span><span style="color: #000000; ">&nbsp;com.island.info;<br /></span><span style="color: #008080; ">&nbsp;2</span>&nbsp;<span style="color: #000000; ">&nbsp;<br /></span><span style="color: #008080; ">&nbsp;3</span>&nbsp;<span style="color: #000000; "></span><span style="color: #008000; ">/**</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">&nbsp;4</span>&nbsp;<span style="color: #008000; ">&nbsp;*&nbsp;&lt;p&gt;Title:&nbsp;TestMaxArray.java&lt;/p&gt;<br /></span><span style="color: #008080; ">&nbsp;5</span>&nbsp;<span style="color: #008000; ">&nbsp;*&nbsp;&lt;p&gt;Description:&nbsp;分治法求解连续和最大&lt;/p&gt;<br /></span><span style="color: #008080; ">&nbsp;6</span>&nbsp;<span style="color: #008000; ">&nbsp;*&nbsp;@date&nbsp;2014-3-05<br /></span><span style="color: #008080; ">&nbsp;7</span>&nbsp;<span style="color: #008000; ">&nbsp;*<br /></span><span style="color: #008080; ">&nbsp;8</span>&nbsp;<span style="color: #008000; ">&nbsp;</span><span style="color: #008000; ">*/</span><span style="color: #000000; "><br /></span><span style="color: #008080; ">&nbsp;9</span>&nbsp;<span style="color: #000000; "></span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">class</span><span style="color: #000000; ">&nbsp;MaxSubArraySum&nbsp;{<br /></span><span style="color: #008080; ">10</span>&nbsp;<span style="color: #000000; ">&nbsp;<br /></span><span style="color: #008080; ">11</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">private</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">static</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">long</span><span style="color: #000000; ">&nbsp;getMax(</span><span style="color: #0000FF; ">long</span><span style="color: #000000; ">&nbsp;a,&nbsp;</span><span style="color: #0000FF; ">long</span><span style="color: #000000; ">&nbsp;b)&nbsp;{<br /></span><span style="color: #008080; ">12</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">&nbsp;a&nbsp;</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; ">&nbsp;b&nbsp;</span><span style="color: #000000; ">?</span><span style="color: #000000; ">&nbsp;a&nbsp;:&nbsp;b;<br /></span><span style="color: #008080; ">13</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080; ">14</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080; ">15</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">/**</span><span style="color: #008000; ">&nbsp;<br /></span><span style="color: #008080; ">16</span>&nbsp;<span style="color: #008000; ">&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;获得连续子数组的最大和&nbsp;<br /></span><span style="color: #008080; ">17</span>&nbsp;<span style="color: #008000; ">&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span><span style="color: #808080; ">@param</span><span style="color: #008000; ">&nbsp;array&nbsp;<br /></span><span style="color: #008080; ">18</span>&nbsp;<span style="color: #008000; ">&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span><span style="color: #808080; ">@return</span><span style="color: #008000; ">&nbsp;最大和，此处用了Long型是为了表示当参数为null或空时，可以返回null，返回其它任何数字都可能引起歧义。&nbsp;<br /></span><span style="color: #008080; ">19</span>&nbsp;<span style="color: #008000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">*/</span><span style="color: #000000; ">&nbsp;<br /></span><span style="color: #008080; ">20</span>&nbsp;<span style="color: #000000; ">&nbsp;<br /></span><span style="color: #008080; ">21</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">static</span><span style="color: #000000; ">&nbsp;Long&nbsp;getMax(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">[]&nbsp;array)&nbsp;{<br /></span><span style="color: #008080; ">22</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080; ">23</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">&nbsp;(array&nbsp;</span><span style="color: #000000; ">==</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">null</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">||</span><span style="color: #000000; ">&nbsp;array.length&nbsp;</span><span style="color: #000000; ">&lt;=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">)&nbsp;{<br /></span><span style="color: #008080; ">24</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">null</span><span style="color: #000000; ">;<br /></span><span style="color: #008080; ">25</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080; ">26</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080; ">27</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">long</span><span style="color: #000000; ">&nbsp;maxSum&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;array[</span><span style="color: #000000; ">0</span><span style="color: #000000; ">];&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">所有子数组中最大的和&nbsp;</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">28</span>&nbsp;<span style="color: #008000; "></span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">long</span><span style="color: #000000; ">&nbsp;righteEdge&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;array[</span><span style="color: #000000; ">0</span><span style="color: #000000; ">];&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">右侧子数组的最大和</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">29</span>&nbsp;<span style="color: #008000; "></span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">&nbsp;(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;i&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;&nbsp;i&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">&nbsp;array.length;&nbsp;i</span><span style="color: #000000; ">++</span><span style="color: #000000; ">)&nbsp;{<br /></span><span style="color: #008080; ">30</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">当右侧子数组的最大和为负数时，对于新数组，右侧子数组的最大和为新增加的数。&nbsp;&nbsp;</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">31</span>&nbsp;<span style="color: #008000; "></span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">&nbsp;(righteEdge&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">)&nbsp;{<br /></span><span style="color: #008080; ">32</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;righteEdge&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;array[i];<br /></span><span style="color: #008080; ">33</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000FF; ">else</span><span style="color: #000000; ">&nbsp;{&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">为正数时，对于新数组，右侧子数组的最大和为新增加的数&nbsp;+&nbsp;原来的最大和。&nbsp;&nbsp;</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">34</span>&nbsp;<span style="color: #008000; "></span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;righteEdge&nbsp;</span><span style="color: #000000; ">+=</span><span style="color: #000000; ">&nbsp;array[i];<br /></span><span style="color: #008080; ">35</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080; ">36</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">所有子数组中最大的和&nbsp;&nbsp;</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">37</span>&nbsp;<span style="color: #008000; "></span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">righteEdge-------------maxSum:</span><span style="color: #000000; ">"</span><span style="color: #000000; ">+</span><span style="color: #000000; ">righteEdge</span><span style="color: #000000; ">+</span><span style="color: #000000; ">"</span><span style="color: #000000; ">****************</span><span style="color: #000000; ">"</span><span style="color: #000000; ">+</span><span style="color: #000000; ">maxSum);<br /></span><span style="color: #008080; ">38</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;maxSum&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;getMax(righteEdge,&nbsp;maxSum);<br /></span><span style="color: #008080; ">39</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080; ">40</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">&nbsp;maxSum;<br /></span><span style="color: #008080; ">41</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080; ">42</span>&nbsp;<span style="color: #000000; ">&nbsp;<br /></span><span style="color: #008080; ">43</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">static</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">&nbsp;main(String[]&nbsp;args)&nbsp;{<br /></span><span style="color: #008080; ">44</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">[]&nbsp;array&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;{</span><span style="color: #000000; ">1</span><span style="color: #000000; ">,&nbsp;</span><span style="color: #000000; ">-</span><span style="color: #000000; ">2</span><span style="color: #000000; ">,&nbsp;</span><span style="color: #000000; ">3</span><span style="color: #000000; ">,&nbsp;</span><span style="color: #000000; ">10</span><span style="color: #000000; ">,&nbsp;</span><span style="color: #000000; ">-</span><span style="color: #000000; ">4</span><span style="color: #000000; ">,&nbsp;</span><span style="color: #000000; ">7</span><span style="color: #000000; ">,&nbsp;</span><span style="color: #000000; ">2</span><span style="color: #000000; ">,&nbsp;</span><span style="color: #000000; ">-</span><span style="color: #000000; ">5</span><span style="color: #000000; ">};<br /></span><span style="color: #008080; ">45</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">int&nbsp;arr[]&nbsp;=&nbsp;{4,-3,5,-2,-1,2,6,-2};&nbsp;&nbsp;</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">46</span>&nbsp;<span style="color: #008000; "></span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">Max&nbsp;sum:&nbsp;</span><span style="color: #000000; ">"</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">+</span><span style="color: #000000; ">&nbsp;MaxSubArraySum.getMax(array));<br /></span><span style="color: #008080; ">47</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080; ">48</span>&nbsp;<span style="color: #000000; ">&nbsp;<br /></span><span style="color: #008080; ">49</span>&nbsp;<span style="color: #000000; ">}</span></div></strong></div><img src ="http://www.blogjava.net/dongisland/aggbug/410637.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/dongisland/" target="_blank">dongisland</a> 2014-03-05 11:47 <a href="http://www.blogjava.net/dongisland/archive/2014/03/05/410637.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>简单理解Ajax原理</title><link>http://www.blogjava.net/dongisland/archive/2014/03/05/410633.html</link><dc:creator>dongisland</dc:creator><author>dongisland</author><pubDate>Wed, 05 Mar 2014 03:27:00 GMT</pubDate><guid>http://www.blogjava.net/dongisland/archive/2014/03/05/410633.html</guid><wfw:comment>http://www.blogjava.net/dongisland/comments/410633.html</wfw:comment><comments>http://www.blogjava.net/dongisland/archive/2014/03/05/410633.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/dongisland/comments/commentRss/410633.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/dongisland/services/trackbacks/410633.html</trackback:ping><description><![CDATA[<span style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px;">Ajax的原理就是:通过javascript的方式，将前台数据通过xmlhttp对象传递到后台，后台在接收到请求后，将需要的结果，再传回到前台，这样就可以实现不需要页面的回发，页是数据实现来回传递，从页实现无刷新。&nbsp;</span><br style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px;" /><span style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px;">Ajax的原理简单来说，实际上就是通过XmlHttpRequest对象来向服务器发异步请求，从服务器获得数据，然后用javascript来操作DOM而更新页面。&nbsp;</span><br style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px;" /><span style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px;">这其中最关键的一步就是从服务器获得请求数据。要清楚这个过程和原理，我们必须对 XMLHttpRequest有所了解。&nbsp;</span><br style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px;" /><span style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px;">我们可以看出，XMLHttpRequest对象完全用来向服务器发出一个请求的，它的作用也局限于此，但它的作用是整个ajax实现的关键，我们可以把服务器端看成一个数据接口，它返回的是一个纯文本流，当然，这个文本流可以是XML格式，可以是Html，可以是Javascript代码，也可以只是一个字符串。这时候，XMLHttpRequest向服务器端请求这个页面，服务器端将文本的结果写入页面，这和普通的web开发流程是一样的，不同的是，客户端在异步获取这个结果后，不是直接显示在页面，而是先由javascript来处理，然后再显示在页面。</span><img src ="http://www.blogjava.net/dongisland/aggbug/410633.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/dongisland/" target="_blank">dongisland</a> 2014-03-05 11:27 <a href="http://www.blogjava.net/dongisland/archive/2014/03/05/410633.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>内外左右连接定义</title><link>http://www.blogjava.net/dongisland/archive/2014/03/05/410631.html</link><dc:creator>dongisland</dc:creator><author>dongisland</author><pubDate>Wed, 05 Mar 2014 03:26:00 GMT</pubDate><guid>http://www.blogjava.net/dongisland/archive/2014/03/05/410631.html</guid><wfw:comment>http://www.blogjava.net/dongisland/comments/410631.html</wfw:comment><comments>http://www.blogjava.net/dongisland/archive/2014/03/05/410631.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/dongisland/comments/commentRss/410631.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/dongisland/services/trackbacks/410631.html</trackback:ping><description><![CDATA[<span style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 10pt; line-height: 26px; background-color: #ffffff;">内连接：INNER&nbsp; JOIN或者JOIN，把两个表中数据对应的数据查出来。&nbsp;</span><br style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;" /><span style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 10pt; line-height: 26px; background-color: #ffffff;">外连接：OUTER&nbsp; JOIN，以某个表为基础把对应数据查出来，分为左外连接和右外连接。&nbsp;</span><br style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;" /><span style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 10pt; line-height: 26px; background-color: #ffffff;">左外连接：LEFT&nbsp; JOIN或者LEFT&nbsp; OUTER&nbsp; JOIN，以某个表为基础把对应数据查出来。&nbsp;</span><br style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;" /><span style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 10pt; line-height: 26px; background-color: #ffffff;">右外连接：RIGHT&nbsp; JOIN或者RIGHT&nbsp; OUTER&nbsp; JOIN，以某个表为基础把对应数据查出来。&nbsp;</span><br style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;" /><span style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 10pt; line-height: 26px; background-color: #ffffff;">全连接：FULL&nbsp; JOIN，以多个表为基础<br /><br /></span><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff; font-size: 10pt;">例子： &nbsp;&nbsp;</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff; font-size: 10pt;">&nbsp; &nbsp;a表 &nbsp; &nbsp; &nbsp;id &nbsp; name &nbsp; &nbsp;</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff; font-size: 10pt;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1 &nbsp; 张3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff; font-size: 10pt;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2 &nbsp; 李四 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff; font-size: 10pt;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3 &nbsp; 王武 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br /></span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff;">&nbsp; &nbsp;&nbsp;</span><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff; font-size: 10pt;">b表 &nbsp; &nbsp; id &nbsp;&nbsp;&nbsp; job &nbsp; parent_id &nbsp;&nbsp;</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff; font-size: 10pt;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1 &nbsp; &nbsp; 23 &nbsp; &nbsp; 1 &nbsp;&nbsp;</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff; font-size: 10pt;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2 &nbsp; &nbsp; 34 &nbsp; &nbsp; 2 &nbsp;&nbsp;</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff; font-size: 10pt;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3 &nbsp; &nbsp; 34 &nbsp; &nbsp; 4 &nbsp;</span><br /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff; font-size: 10pt;">&nbsp; a.id同b.parent_id &nbsp; 存在</span><nobr id="key1" target="_blank" style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; color: #6600ff; border-bottom-width: 0px; border-bottom-style: dotted; text-decoration: underline;"><span style="font-size: 10pt;">关系</span></nobr><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff;">&nbsp; &nbsp;<br /></span><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff; color: red; font-size: 10pt;">内连接</span><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff; font-size: 10pt;">&nbsp; select &nbsp; a.*,b.* &nbsp; from &nbsp; a &nbsp; inner &nbsp; join &nbsp; b &nbsp; &nbsp; on &nbsp; a.id=b.parent_id &nbsp;&nbsp;</span><br /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff; font-size: 10pt;">&nbsp;结果是 &nbsp; &nbsp;&nbsp;</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff; font-size: 10pt;">&nbsp; 1 &nbsp; 张3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; &nbsp; 23 &nbsp; &nbsp; 1 &nbsp;&nbsp;</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff; font-size: 10pt;">&nbsp; 2&nbsp;&nbsp; 李四 &nbsp; &nbsp; &nbsp; &nbsp; 2 &nbsp; &nbsp; 34 &nbsp; &nbsp; 2 &nbsp;&nbsp;</span><br />&nbsp;<span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff; color: red; font-size: 10pt;">左连接</span><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff; font-size: 10pt;">&nbsp; select &nbsp; a.*,b.* &nbsp; from &nbsp; a &nbsp; left &nbsp; join &nbsp; b &nbsp; &nbsp; on &nbsp; a.id=b.parent_id &nbsp;&nbsp;</span><br /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff; font-size: 10pt;">结果是 &nbsp; &nbsp;&nbsp;</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff; font-size: 10pt;">&nbsp; 1 &nbsp; 张3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1 &nbsp; &nbsp; 23 &nbsp; &nbsp; 1 &nbsp;&nbsp;</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff; font-size: 10pt;">&nbsp; 2 &nbsp; 李四 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2 &nbsp; &nbsp; 34 &nbsp; &nbsp; 2 &nbsp;&nbsp;</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; background-color: #ffffff; font-size: 10pt;">&nbsp; 3 &nbsp; 王武 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;null &nbsp;</span><br /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; color: red; font-size: 10pt;">右连接&nbsp;</span><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px;">&nbsp;&nbsp;</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; font-size: 10pt;">&nbsp; select &nbsp; a.*,b.* &nbsp; from &nbsp; a &nbsp; right &nbsp; join &nbsp; b &nbsp; &nbsp; on &nbsp; a.id=b.parent_id &nbsp;&nbsp;</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; font-size: 10pt;">&nbsp; 结果是 &nbsp; &nbsp;&nbsp;</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; font-size: 10pt;">&nbsp; 1 &nbsp; 张3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; &nbsp; 23 &nbsp; &nbsp; 1 &nbsp;&nbsp;</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; font-size: 10pt;">&nbsp; 2 &nbsp; 李四 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2 &nbsp; &nbsp; 34 &nbsp; &nbsp; 2 &nbsp;&nbsp;</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; font-size: 10pt;">&nbsp; null &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3 &nbsp; &nbsp; 34 &nbsp; &nbsp; 4&nbsp;&nbsp;&nbsp;</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px;">&nbsp;&nbsp;</span><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; color: red; font-size: 10pt;">完全连接&nbsp;&nbsp;</span>&nbsp;<br style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; font-size: 10pt;">&nbsp; select &nbsp; a.*,b.* &nbsp; from &nbsp; a &nbsp; full &nbsp; join &nbsp; b &nbsp; &nbsp; on &nbsp; a.id=b.parent_id &nbsp;</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; font-size: 10pt;">&nbsp; 结果是 &nbsp; &nbsp;&nbsp;</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; font-size: 10pt;">&nbsp; 1 &nbsp; 张3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; &nbsp; 23 &nbsp; &nbsp; 1 &nbsp;&nbsp;</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; font-size: 10pt;">&nbsp; 2 &nbsp; 李四 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2 &nbsp; &nbsp; 34 &nbsp; &nbsp; 2 &nbsp;&nbsp;</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; font-size: 10pt;">&nbsp; null &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3 &nbsp; &nbsp; 34 &nbsp; &nbsp; 4 &nbsp;&nbsp;</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 25.200000762939453px; font-size: 10pt;">&nbsp; 3 &nbsp; 王武 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null</span><img src ="http://www.blogjava.net/dongisland/aggbug/410631.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/dongisland/" target="_blank">dongisland</a> 2014-03-05 11:26 <a href="http://www.blogjava.net/dongisland/archive/2014/03/05/410631.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>浅谈JAVA集合 Map List Set等java集合类</title><link>http://www.blogjava.net/dongisland/archive/2014/03/05/410628.html</link><dc:creator>dongisland</dc:creator><author>dongisland</author><pubDate>Wed, 05 Mar 2014 03:07:00 GMT</pubDate><guid>http://www.blogjava.net/dongisland/archive/2014/03/05/410628.html</guid><wfw:comment>http://www.blogjava.net/dongisland/comments/410628.html</wfw:comment><comments>http://www.blogjava.net/dongisland/archive/2014/03/05/410628.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/dongisland/comments/commentRss/410628.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/dongisland/services/trackbacks/410628.html</trackback:ping><description><![CDATA[<p style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;"><span style="font-size: 10pt;"><strong>一、Collections类和Collection接口</strong></span></p><p style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;"></p><p style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;"><span style="font-family: Calibri; font-size: 10pt;">&nbsp; &nbsp; &nbsp;Collections</span><span style="font-size: 10pt;">是针对集合类的一个帮助类，他提供一系列静态方法实现对各种集合的搜索、排序、线程安全化等操作。</span></p><p style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;"><span style="font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;Collection是最基本的集合接口，一个Collection代表一组Object，即Collection的元素（Elements）。一些 Collection允许相同的元素而另一些不行。一些能排序而另一些不行。Java&nbsp;SDK不提供直接继承自Collection的 类，Java&nbsp;SDK提供的类都是继承自Collection的&#8220;子接口&#8221;如List和Set。</span><br /></p><p style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;"><strong style="font-size: 10pt;">二、现在来谈谈Java集合的一些实现类。</strong></p><p style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;"><span style="color: black;"><span style="font-family: Calibri; font-size: 10pt;">Collection<br /></span></span><span style="font-size: 10pt;">&#9500;</span><span style="color: black;"><span style="font-family: Calibri; font-size: 10pt;">List</span></span></p><p style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;"><span style="color: black;"><span style="font-family: Calibri; font-size: 10pt;">&#9474;</span></span><span style="font-size: 10pt;">&#9500;</span><span style="color: black;"><span style="font-family: Calibri; font-size: 10pt;">ArreyList&nbsp;</span></span></p><p style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;"><span style="color: black;"><span style="font-family: Calibri; font-size: 10pt;">&#9474;</span></span><span style="font-size: 10pt;">&#9500;</span><span style="color: black;"><span style="font-family: Calibri; font-size: 10pt;">Vector</span></span></p><p style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;"><span style="color: black;"><span style="font-family: Calibri; font-size: 10pt;">&#9474;</span></span><span style="font-size: 10pt;">&#9500;</span><span style="color: black;"><span style="font-family: Calibri; font-size: 10pt;">LinkedList</span></span></p><p style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;"><span style="color: black;"><span style="font-family: Calibri; font-size: 10pt;">&#9474;&#9492;Stack</span></span></p><p style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;"><span style="color: black;"><span style="font-family: Calibri; font-size: 10pt;">&#9492;Set</span></span></p><p style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;"><span style="color: black;"><span style="font-family: Calibri; font-size: 10pt;">&#9474;</span></span><span style="font-size: 10pt;">&#9500;</span><span style="color: black;"><a title="java.util 中的类" style="color: #005880;"><span style="color: black;"><span style="font-family: Calibri; font-size: 10pt;"><u>HashSet</u></span></span></a><br /><span style="font-family: Calibri; font-size: 10pt;">&#9474;</span></span><span style="font-size: 10pt;">&#9500;</span><span style="color: black;"><a title="java.util 中的类" style="color: #005880;"><span style="color: black;"><span style="font-family: Calibri; font-size: 10pt;"><u>LinkedHashSet</u></span></span></a></span></p><p style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;"><span style="color: black;"><span style="font-family: Calibri; font-size: 10pt;">&#9474;&#9492;</span><a title="java.util 中的类" style="color: #005880;"><span style="color: black;"><span style="font-family: Calibri; font-size: 10pt;"><u>TreeSet</u></span></span></a></span></p><p style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;"><span style="color: black;"><span style="font-family: Calibri; font-size: 10pt;"><strong>List代表有序、重复的集合</strong></span></span></p><p style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;"><span style="color: black;"></span></p><p style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;"><span style="font-size: 10pt;">1.ArrayList类</span><br /><span style="font-size: 10pt;">　　ArrayList实现了可变大小的数组。它允许所有元素，包括null。ArrayList没有同步。</span><br /><span style="font-size: 10pt;">size，isEmpty，get，set方法运行时间为常数。但是add方法开销为分摊的常数，添加n个元素需要O(n)的时间。其他的方法运行时间为线性。</span><br /><span style="font-size: 10pt;">　　每个ArrayList实例都有一个容量（Capacity），即用于存储元素的数组的大小。这个容量可随着不断添加新元素而自动增加，但是增长算法 并没有定义。当需要插入大量元素时，在插入前可以调用ensureCapacity方法来增加ArrayList的容量以提高插入效率。</span><br /><span style="font-size: 10pt;">　　和LinkedList一样，ArrayList也是非同步的（unsynchronized）。</span></p><div class="post"><div class="postbody" style="margin: 10px 15px 5px 0px; line-height: 26px;"><div id="blog_content"><p><span style="font-size: 10pt;">2.Vector类</span><br /><span style="font-size: 10pt;">　　Vector非常类似ArrayList，但是Vector是同步的。由Vector创建的Iterator，虽然和ArrayList创建的Iterator是同一接口，但是，因为Vector是同步的，当一个Iterator被创建而且正在被使用，另一个线程改变了Vector的状态（例如，添加或删除了一些元素），这时调用Iterator的方法时将抛出ConcurrentModificationException，因此必须捕获该异常。</span></p><p><span style="font-size: 10pt;">3.LinkedList类</span><br /><span style="font-size: 10pt;">　　LinkedList实现了List接口，允许null元素。此外LinkedList提供额外的get，remove，insert方法在 LinkedList的首部或尾部。这些操作使LinkedList可被用作堆栈（stack），队列（queue）或双向队列（deque）。</span><br /><span style="font-size: 10pt;">　　注意LinkedList没有同步方法。如果多个线程同时访问一个List，则必须自己实现访问同步。一种解决方法是在创建List时构造一个同步的List：</span><br /><span style="font-size: 10pt;">　　　　List&nbsp;list&nbsp;=&nbsp;Collections.synchronizedList(new&nbsp;LinkedList(...));</span></p><p><span style="font-size: 10pt;">4.Stack&nbsp;类</span><br /><span style="font-size: 10pt;">　　Stack继承自Vector，实现一个后进先出的堆栈。Stack提供5个额外的方法使得Vector得以被当作堆栈使用。基本的push和pop方法，还有peek方法得到栈顶的元素，empty方法测试堆栈是否为空，search方法检测一个元素在堆栈中的位置。Stack刚创建后是空栈。</span></p><p><span style="font-size: 10pt;"><strong>Set代表无序、不可重复的集合</strong><br /><strong>HashSet和TreeSet的区别</strong><br />HashSet无序，TreeSet有序，两者均不能有重复的对象<br /></span></p><p><span style="font-family: Calibri; font-size: 10pt;">Map<br /></span><span style="font-size: 10pt;">&#9500;</span><span style="font-family: Calibri; font-size: 10pt;">HashMap</span></p><p><span style="font-size: 10pt;">&#9500;</span><span style="font-family: Calibri; font-size: 10pt;">Hashtable</span></p><p><span style="font-size: 10pt;">&#9500;</span><a title="java.util 中的类" style="color: #005880;"><span style="color: black;"><span style="font-family: Calibri; font-size: 10pt;"><u>TreeMap</u></span></span></a><br /><span style="font-family: Calibri; font-size: 10pt;">&#9492;WeakHashMap</span></p><p><span style="font-size: 10pt;">Map没有继承Collection接口</span></p><p><span style="font-size: 10pt;">1.Hashtable类</span><br /><span style="font-size: 10pt;">　　Hashtable继承Map接口，实现一个key-value映射的哈希表。任何非空（non-null）的对象都可作为key或者value。</span><span style="font-size: 10pt;">Hashtable是同步的。</span></p><p><span style="font-size: 10pt;">2.HashMap类</span><br /><span style="font-size: 10pt;">　　HashMap和Hashtable类似，不同之处在于 HashMap是非同步的，并且允许null，即null&nbsp;value和null&nbsp;key。，但是将HashMap视为Collection时 （values()方法可返回Collection），其迭代子操作时间开销和HashMap的容量成比例。因此，如果迭代操作的性能相当重要的话，不要 将HashMap的初始化容量设得过高，或者load&nbsp;factor过低。</span><br /><br /><span style="font-size: 10pt;">3.WeakHashMap类</span><br /><span style="font-size: 10pt;">　　WeakHashMap是一种改进的HashMap，它对key实行&#8220;弱引用&#8221;，如果一个key不再被外部所引用，那么该key可以被GC回收。</span></p><p><strong style="font-size: 10pt;">三、集合类的遍历</strong></p><p><span style="font-size: 10pt;">遍历通用<span style="font-family: Calibri; font-size: 10pt;">Collection</span></span></p><p><span style="font-size: 10pt;">Iterator&nbsp;it&nbsp;=&nbsp;collection.iterator();&nbsp;//&nbsp;获得一个迭代子</span><br /><span style="font-size: 10pt;">　　while(it.hasNext())&nbsp;{</span><br /><span style="font-size: 10pt;">　　　Object&nbsp;obj&nbsp;=&nbsp;it.next();&nbsp;//&nbsp;得到下一个元素</span><br /><span style="font-size: 10pt;">}</span></p></div></div></div><img src ="http://www.blogjava.net/dongisland/aggbug/410628.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/dongisland/" target="_blank">dongisland</a> 2014-03-05 11:07 <a href="http://www.blogjava.net/dongisland/archive/2014/03/05/410628.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Struts1与Struts2的区别</title><link>http://www.blogjava.net/dongisland/archive/2014/03/05/410627.html</link><dc:creator>dongisland</dc:creator><author>dongisland</author><pubDate>Wed, 05 Mar 2014 02:52:00 GMT</pubDate><guid>http://www.blogjava.net/dongisland/archive/2014/03/05/410627.html</guid><wfw:comment>http://www.blogjava.net/dongisland/comments/410627.html</wfw:comment><comments>http://www.blogjava.net/dongisland/archive/2014/03/05/410627.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/dongisland/comments/commentRss/410627.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/dongisland/services/trackbacks/410627.html</trackback:ping><description><![CDATA[<span style="color: #4d4d4d; font-size: 13px; line-height: 26px; font-family: Arial; background-color: #ffffff;">1) 在Action实现类方面的对比：Struts 1要求Action类继承一个抽象基类；Struts 1的一个具体问题是使用抽象类编程而不是接口。Struts 2 Action类可以实现一个Action接口，也可以实现其他接口，使可选和定制的服务成为可能。Struts 2提供一个ActionSupport基类去实现常用的接口。即使Action接口不是必须实现的，只有一个包含execute方法的POJO类都可以用作Struts 2的Action。&nbsp;</span><br style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;" /><span style="color: #4d4d4d; font-size: 13px; line-height: 26px; font-family: Arial; background-color: #ffffff;">2) 线程模式方面的对比：Struts 1 Action是单例模式并且必须是线程安全的，因为仅有Action的一个实例来处理所有的请求。单例策略限制了Struts 1 Action能做的事，并且要在开发时特别小心。Action资源必须是线程安全的或同步的；Struts 2 Action对象为每一个请求产生一个实例，因此没有线程安全问题。 即struts1每个action只有1个实例，struts2每一个请求都会new一个实例，所以struts2是线程安全的</span><br style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;" /><span style="color: #4d4d4d; font-size: 13px; line-height: 26px; font-family: Arial; background-color: #ffffff;">3) Servlet依赖方面的对比：Struts 1 Action依赖于Servlet API，因为Struts 1 Action的execute方法中有HttpServletRequest和HttpServletResponse方法。Struts 2 Action不再依赖于Servlet API，从而允许Action脱离Web容器运行，从而降低了测试Action的难度。 当然，如果Action需要直接访问HttpServletRequest和HttpServletResponse参数，Struts 2 Action仍然可以访问它们。但是，大部分时候，Action都无需直接访问HttpServetRequest和HttpServletResponse，从而给开发者更多灵活的选择。&nbsp;</span><br style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;" /><span style="color: #4d4d4d; font-size: 13px; line-height: 26px; font-family: Arial; background-color: #ffffff;">4) 可测性方面的对比：测试Struts 1 Action的一个主要问题是execute方法依赖于Servlet API，这使得Action的测试要依赖于Web容器。为了脱离Web容器测试Struts 1的Action，必须借助于第三方扩展：Struts TestCase，该扩展下包含了系列的Mock对象（模拟了HttpServetRequest和HttpServletResponse对象），从而可以脱离Web容器测试Struts 1的Action类。Struts 2 Action可以通过初始化、设置属性、调用方法来测试。&nbsp;</span><br style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;" /><span style="color: #4d4d4d; font-size: 13px; line-height: 26px; font-family: Arial; background-color: #ffffff;">5) 封装请求参数的对比：Struts 1使用ActionForm对象封装用户的请求参数，所有的ActionForm必须继承一个基类：ActionForm。普通的JavaBean不能用作ActionForm，因此，开发者必须创建大量的ActionForm类封装用户请求参数。虽然Struts 1提供了动态ActionForm来简化ActionForm的开发，但依然需要在配置文件中定义ActionForm；Struts 2直接使用Action属性来封装用户请求属性，避免了开发者需要大量开发ActionForm类的烦琐，实际上，这些属性还可以是包含子属性的Rich对象类型。如果开发者依然怀念Struts 1 ActionForm的模式，Struts 2提供了ModelDriven模式，可以让开发者使用单独的Model对象来封装用户请求参数，但该Model对象无需继承任何Struts 2基类，是一个POJO，从而降低了代码污染。&nbsp;</span><br style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;" /><span style="color: #4d4d4d; font-size: 13px; line-height: 26px; font-family: Arial; background-color: #ffffff;">6) 表达式语言方面的对比：Struts 1整合了JSTL，因此可以使用JSTL表达式语言。这种表达式语言有基本对象图遍历，但在对集合和索引属性的支持上则功能不强；Struts 2可以使用JSTL，但它整合了一种更强大和灵活的表达式语言：OGNL（Object Graph Notation Language），因此，Struts 2下的表达式语言功能更加强大。&nbsp;</span><br style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;" /><span style="color: #4d4d4d; font-size: 13px; line-height: 26px; font-family: Arial; background-color: #ffffff;">7) &#8212; 绑定值到视图的对比：Struts 1使用标准JSP机制把对象绑定到视图页面；Struts 2使用&#8220;ValueStack&#8221;技术，使标签库能够访问值，而不需要把对象和视图页面绑定在一起。&nbsp;</span><br style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;" /><span style="color: #4d4d4d; font-size: 13px; line-height: 26px; font-family: Arial; background-color: #ffffff;">8) 类型转换的对比：Struts 1 ActionForm 属性通常都是String类型。Struts 1使用Commons-Beanutils进行类型转换，每个类一个转换器，转换器是不可配置的；Struts 2使用OGNL进行类型转换，支持基本数据类型和常用对象之间的转换。&nbsp;</span><br style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;" /><span style="color: #4d4d4d; font-size: 13px; line-height: 26px; font-family: Arial; background-color: #ffffff;">9) 数据校验的对比：Struts 1支持在ActionForm重写validate方法中手动校验，或者通过整合Commons alidator框架来完成数据校验。Struts 2支持通过重写validate方法进行校验，也支持整合XWork校验框架进行校验。&nbsp;</span><br style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;" /><span style="color: #4d4d4d; font-size: 13px; line-height: 26px; font-family: Arial; background-color: #ffffff;">10) Action执行控制的对比：Struts 1支持每一个模块对应一个请求处理（即生命周期的概念），但是模块中的所有Action必须共享相同的生命周期。Struts 2支持通过拦截器堆栈（Interceptor Stacks）为每一个Action创建不同的生命周期。开发者可以根据需要创建相应堆栈，从而和不同的Action一起使用。&nbsp;</span><br style="color: #4d4d4d; font-family: Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 26px; background-color: #ffffff;" /><span style="color: #4d4d4d; font-size: 13px; line-height: 26px; font-family: Arial; background-color: #ffffff;">11) 捕获输入：Struts1 使用ActionForm对象捕获输入。所有的ActionForm必须继承一个基类。因为其他JavaBean不能用作ActionForm，开发者经常创建多余的类捕获输入。动态Bean（DynaBeans）可以作为创建传统ActionForm的选择，但是，开发者可能是在重新描述(创建)已经存在的JavaBean（仍然会导致有冗余的javabean）。Struts 2直接使用Action属性作为输入属性，消除了对第二个输入对象的需求。输入属性可能是有自己(子)属性的rich对象类型。Action属性能够通过 web页面上的taglibs访问。Struts2也支持ActionForm模式。rich对象类型，包括业务对象，能够用作输入/输出对象。这种 ModelDriven 特性简化了taglib对POJO输入对象的引用。</span><span style="color: #4d4d4d; font-size: 13px; line-height: 26px; font-family: Arial; background-color: #ffffff;"><br /></span><img src ="http://www.blogjava.net/dongisland/aggbug/410627.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/dongisland/" target="_blank">dongisland</a> 2014-03-05 10:52 <a href="http://www.blogjava.net/dongisland/archive/2014/03/05/410627.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>