पहले वहाँ अपने प्रश्न के साथ एक समस्या है ", स्ट्रिंगबफर/स्ट्रिंगबिल्डर के अंदर कोई ऑब्जेक्ट नहीं बनाया जाएगा यदि हम बफर को ओवरफ़्लो नहीं करते हैं। तो कोड की उन पंक्तियों 2 या 3 वस्तुओं बनाते हैं।
StringBuilder s = new StringBuilder("Orange");
s.append("apple");
अब यहाँ केवल एक ही वस्तु
बनाई गई है मैं नहीं जानता कि जहां कि, यहाँ आप एक StringBuilder वस्तु, एक "ऑरेंज" स्ट्रिंग, एक "सेब" स्ट्रिंग बनाने मिलता है , कुल 3 ऑब्जेक्ट्स के लिए, या 4 अगर हम स्ट्रिंगबिल्डर बफर ओवरफ्लो करते हैं। (मैं वस्तु निर्माण के रूप में सरणी निर्माण गिनती)।
मैं के रूप में अपने प्रश्न पढ़ें, कैसे StringBuilder एक नई वस्तु (जब बफर बहकर नहीं है) बनाने के बिना संलग्न कर सकते हैं?
आपको StringBuilder
पर देखना चाहिए, क्योंकि यह गैर थ्रेड सुरक्षित कार्यान्वयन है।कोड दिलचस्प और पढ़ने के लिए आसान है। मैंने इनलाइन टिप्पणियां जोड़ दी हैं।
आंतरिक संरचना के रूप में वहाँ एक चार सरणी, नहीं एक स्ट्रिंग है। यह शुरुआत में लंबाई 16 के साथ बनाया गया है और क्षमता पार होने पर हर बार बढ़ाई जाएगी। यदि तारों को चार सरणी के भीतर फिट करने के लिए स्ट्रिंग्स हैं, तो नए ऑब्जेक्ट्स बनाने की कोई आवश्यकता नहीं है।
StringBuilder
फैली AbstractStringBuilder
है, जहां आप निम्नलिखित कोड मिल जाएगा:
/**
* The count is the number of characters used.
*/
int count;
:
/**
* The value is used for character storage.
*/
char value[];
के बाद से नहीं सभी सरणी एक निश्चित समय पर उपयोग किया जाएगा, एक और महत्वपूर्ण चर लंबाई है
संलग्न करने के कई अधिभार हैं, लेकिन सबसे दिलचस्प एक निम्नलिखित है:
public AbstractStringBuilder append(String str) {
if (str == null) str = "null"; //will literally append "null" in case of null
int len = str.length(); //get the string length
if (len == 0) return this; //if it's zero, I'm done
int newCount = count + len; //tentative new length
if (newCount > value.length) //would the new length fit?
expandCapacity(newCount); //oops, no, resize my array
str.getChars(0, len, value, count); //now it will fit, copy the chars
count = newCount; //update the count
return this; //return a reference to myself to allow chaining
}
String.getChars (पूर्णांक srcBegin, पूर्णांक srcEnd, चार [] डीएसटी, पूर्णांक dstBegin) गंतव्य चरित्र सरणी में इस स्ट्रिंग से प्रतियां अक्षर।
तो, संलग्न विधि काफी सरल है, केवल जादू की खोज करने expandCapacity
है छोड़ दिया है, यहाँ यह है:
void expandCapacity(int minimumCapacity) {
//get the current length add one and double it
int newCapacity = (value.length + 1) * 2;
if (newCapacity < 0) { //if we had an integer overflow
newCapacity = Integer.MAX_VALUE; //just use the max positive integer
} else if (minimumCapacity > newCapacity) { //is it enough?
//if doubling wasn't enough, use the actual length computed
newCapacity = minimumCapacity;
}
//copy the old value in the new array
value = Arrays.copyOf(value, newCapacity);
}
Arrays.copyOf (चार [] मूल, पूर्णांक newLength) प्रतियां निर्दिष्ट सरणी, छंटनी या नल वर्णों के साथ पैडिंग (यदि आवश्यक हो) तो प्रतिलिपि निर्दिष्ट लंबाई है।
हमारे मामले, गद्दी में, हम लंबाई का विस्तार कर रहे के बाद से।
सवाल में कुछ मान्यताओं जो सही नहीं हैं कर रहे हैं। 'नया स्ट्रिंगबिल्डर()' और 'नया स्ट्रिंग()' दो ऑब्जेक्ट बनाता है। –
क्या यह मेरा सवाल था? ;) –