stringbuffer

What is the complexity of this simple piece of code?

ε祈祈猫儿з 提交于 2019-12-27 22:08:17
问题 I'm pasting this text from an ebook I have. It says the complexity if O(n 2 ) and also gives an explanation for it, but I fail to see how. Question: What is the running time of this code? public String makeSentence(String[] words) { StringBuffer sentence = new StringBuffer(); for (String w : words) sentence.append(w); return sentence.toString(); } The answer the book gave: O(n 2 ), where n is the number of letters in sentence. Here’s why: each time you append a string to sentence, you create

Removing Parts of File names in Folder

大兔子大兔子 提交于 2019-12-24 09:29:02
问题 The code below works, but my problem is that the console output shows correctly for example: 3-M-ALABAMA-SUIQUARTER2 3-M-ALABAMA-SUIQUARTER2 3-M-ALABAMAW-22017 3-M-ALABAMAW-22017 The output above show that my index is -2017 however when the actual file name is being change in the folder some of the File Names are skipped. For example Orginal file name: 3-M-ALABAMA-SUIQUARTER2-2017200346-CD6140 Console Output: 3-M-ALABAMA-SUIQUARTER2 Some of Files in folder unchanged: 3-M-ALABAMA-SUIQUARTER2

Stringbuffer,Stringbuilder when to use? [duplicate]

筅森魡賤 提交于 2019-12-24 09:26:56
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: StringBuilder and StringBuffer in Java Criteria to choose among StringBuffer and StringBuilder 回答1: If you're definitely using Java 5 or higher, and you definitely don't need to share the object between threads (I can't remember ever needing to do so), StringBuilder is a better bet. Basically you should almost always use StringBuilder when you can, to avoid pointless synchronization. Admittedly the way most VMs

Stringbuffer,Stringbuilder when to use? [duplicate]

懵懂的女人 提交于 2019-12-24 09:20:10
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: StringBuilder and StringBuffer in Java Criteria to choose among StringBuffer and StringBuilder 回答1: If you're definitely using Java 5 or higher, and you definitely don't need to share the object between threads (I can't remember ever needing to do so), StringBuilder is a better bet. Basically you should almost always use StringBuilder when you can, to avoid pointless synchronization. Admittedly the way most VMs

StringBuffer is obsolete?

一世执手 提交于 2019-12-20 10:16:11
问题 In the book "Effective Java", Josh Bloch says that StringBuffer is largely obsolete and should be replaced by the non-synchronized implementation 'StringBuilder' . But in my experience, I've still seen widespread use of the StringBuffer class. Why is the StringBuffer class now obsolete and why should StringBuilder be preferred over StringBuffer except for the increased performance due to non-synchronization? 回答1: It's obsolete in that new code on Java 1.5 should generally use StringBuilder -

A Set in java never allows duplicates, but it takes StringBuffer objects with the same argument. Why?

给你一囗甜甜゛ 提交于 2019-12-19 07:01:52
问题 public static void main(String[] args) { HashSet set = new HashSet(); set.add(new StringBuffer("abc")); set.add(new StringBuffer("abc")); set.add(new StringBuffer("abc")); set.add(new StringBuffer("abc")); System.out.println(set); } Output: [abc,abc,abc,abc] Here in above code I added object of StringBuffer("abc") many times and Set adds it but Set never adds duplicates. 回答1: StringBuffer does not override Object#equals() and Object#hashCode(), so identity of StringBuffer instances is based

Does the StringBuffer equals method compare content? [duplicate]

只愿长相守 提交于 2019-12-19 03:13:28
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Comparing StringBuffer content with equals StringBuffer s1= new StringBuffer("Test"); StringBuffer s2 = new StringBuffer("Test"); if(s1.equals(s2)) { System.out.println("True"); } else { System.out.println("False"); } Why does that code print "False"? 回答1: StringBuffer does not override the Object.equals method, so it is not performing a string comparison. Instead it is performing a direct object comparison.

What does “ StringBuilders are not thread-safe” mean?

廉价感情. 提交于 2019-12-18 13:28:39
问题 I have read some articles about the pros and cons of String and StringBuilder in the Java Programming language. In one of the articles, the author mentioned that: StringBuilder is not Thread-safe , so in multiple threads use StringBuffer . Unfortunately, I cannot understand what it means. Could you please explain the difference between String , StringBuilder and StringBuffer especially in the context of "Thread safety". I would appreciate it if you could describe with the code example. 回答1:

Why use StringBuilder? StringBuffer can work with multiple thread as well as one thread?

南楼画角 提交于 2019-12-18 10:59:23
问题 Suppose our application have only one thread. and we are using StringBuffer then what is the problem? I mean if StringBuffer can handle multiple threads through synchronization, what is the problem to work with single thread? Why use StringBuilder instead? 回答1: StringBuffers are thread-safe, meaning that they have synchronized methods to control access so that only one thread can access a StringBuffer object's synchronized code at a time. Thus, StringBuffer objects are generally safe to use

When does StringBuffer adds strings to the String Pool?

こ雲淡風輕ζ 提交于 2019-12-18 07:24:31
问题 When I define a StringBuffer variable with new , this string is not added to the String pool, right? Now, when I define another StringBuffer but not with new , I define it as StrPrev.append("XXX") suddenly it is.(or so says my college teacher). Why is that? What makes this string to suddenly become a string-pool string? 回答1: When I define a StringBuffer variable with new, this string is not added to the String pool, right? Creating a StringBuffer does not create a String at all. Now, when I