jQuery -> 擷取指定上下文中的DOM元素

來源:互聯網
上載者:User

標籤:style   blog   class   c   code   java   

作者 : 卿篤軍


一個String對象的長度是固定的,不能改變它的內容,或者是附加新的字元至String對象中。您也許會使用+來串聯字串以達到附加新字元或字串 的目的,但+會產生一個新的String執行個體。如果程式對這種附加字串的需求很頻繁,並不建議使用+來進行字串的串聯。在物件導向程式設計中,最好是 能重複運用已產生的對象,對象的產生需要記憶體空間與時間,不斷地產生String執行個體是一個沒有效率的行為。J2SE 5.0提供java.lang.StringBuilder類,使用這個類所產生的對象預設會有16個字元的長度,您也可以自行指定初始長度。如果附加的 字元超出可容納的長度,則StringBuilder對象會自動增加長度以容納被附加的字元。如果有頻繁作字串附加的需求,使用 StringBuilder會讓程式的效率大大提高。通過下面的簡單測試程式就可以知道效能差距有多大。


樣本:String 與 StringBuilder 效率體現

package append;public class Append{public static void main(String[] args) {// TODO Auto-generated method stubString str = "";long sTime = System.currentTimeMillis();for (int i = 0; i < 10000; ++i)str += i;long eTime = System.currentTimeMillis();System.out.println("消耗時間1:"+(eTime-sTime));StringBuilder builder = new StringBuilder("");   //建立字串產生器sTime = System.currentTimeMillis();for (int i = 0; i < 10000; ++i)builder.append(i);eTime = System.currentTimeMillis();System.out.println("消耗時間2:"+(eTime-sTime));}}
運行結果:

消耗時間1:762
消耗時間2:3




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.