stringbuffer

Converting Char Array to List in Java

纵饮孤独 提交于 2019-11-28 08:09:22
Can anyone help me and tell how to convert a char array to a list and vice versa. I am trying to write a program in which users enters a string (e.g "Mike is good" ) and in the output, each whitespace is replaced by "%20" (I.e "Mike%20is%20good" ). Although this can be done in many ways but since insertion and deletion take O(1) time in linked list I thought of trying it with a linked list. I am looking for someway of converting a char array to a list, updating the list and then converting it back. public class apples { public static void main(String args[]) { Scanner input = new Scanner

stringstream->rdbuf()->pubsetbuf is not setting the buffer

邮差的信 提交于 2019-11-28 07:31:28
问题 I am trying to modify a stringbuffer of a stringstream object without having to copy a string, using the method pubsetbuf, but it is not working. I am following the documentation in http://www.cplusplus.com/reference/iostream/streambuf/pubsetbuf/. Here is my example code: #include <iostream> #include <sstream> int main(int argc, char* argv[]) { std::stringstream stream("You say goodbye"); char replace[] = {"And I say hello"}; std::cout << stream.str() << std::endl; // Checking original

What is difference between mutable and immutable String in java

泪湿孤枕 提交于 2019-11-28 03:39:56
As per my knowledge, a mutable string can be changed, and an immutable string cannot be changed. Here I want to change the value of String like this, String str="Good"; str=str+" Morning"; and other way is, StringBuffer str= new StringBuffer("Good"); str.append(" Morning"); In both the cases I am trying to alter the value of str . Can anyone tell me, what is difference in both case and give me clear picture of mutable and immutable objects. TheLostMind Case 1: String str = "Good"; str = str + " Morning"; In the above code you create 3 String Objects. "Good" it goes into the String Pool . "

What is the capacity of a StringBuffer?

守給你的承諾、 提交于 2019-11-28 03:02:45
问题 When I run this code: StringBuffer name = new StringBuffer("stackoverflow.com"); System.out.println("Length: " + name.length() + ", capacity: " + name.capacity()); it gives output: Length: 17, capacity: 33 Obvious length is related to number of characters in string, but I am not sure what capacity is? Is that number of characters that StringBuffer can hold before reallocating space? 回答1: See: JavaSE 6 java.lang.StringBuffer capacity() But your assumption is correct: The capacity is the amount

replaceAll does not replace string [duplicate]

主宰稳场 提交于 2019-11-28 01:58:20
This question already has an answer here: String replace method is not replacing characters 5 answers I want the text "REPLACEME" to be replaced with my StringBuffer symbols. When I print symbols, it is a valid string. When I print my query, it still has the text REPLACEME instead of symbols. Why? private String buildQuery(){ String query = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(REPLACEME)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback="; deserializeQuotes(); StringBuffer symbols = new

Python equivalent of Java StringBuffer?

人走茶凉 提交于 2019-11-27 18:39:54
Is there anything in Python like Java's StringBuffer ? Since strings are immutable in Python too, editing them in loops would be inefficient. Efficient String Concatenation in Python is a rather old article and its main statement that the naive concatenation is far slower than joining is not valid anymore, because this part has been optimized in CPython since then: CPython implementation detail: If s and t are both strings, some Python implementations such as CPython can usually perform an in-place optimization for assignments of the form s = s + t or s += t. When applicable, this optimization

Does “+” use in String concatenation affect efficiency? [duplicate]

假装没事ソ 提交于 2019-11-27 08:01:21
问题 This question already has answers here : StringBuilder vs String concatenation in toString() in Java (18 answers) How Java do the string concatenation using “+”? (6 answers) Closed 5 years ago . I have worked with String, StringBuilder and StringBuffer in java. I thought of this question, while I was thinking from efficiency point of view. Does "+" use in String concatenation affect efficiency? 回答1: Yes, but so little it shouldn't matter most of the time. Using '+' for string constants is the

How to get address of a Java Object? [duplicate]

流过昼夜 提交于 2019-11-27 05:06:34
This question already has an answer here: Is there a way to get a reference address? [duplicate] 5 answers Is there a way to get address of a Java object? Where the question comes from?: At First, I read properties file and all the data from file was placed into table. Properties file can update. So, I want to listen that file. I listen an object using PropertyChangeSupport and PropertyChangeListener. updatedStatus = new basit.data.MyString(); updatedStatus.addPropertyChangeListener(new java.beans.PropertyChangeListener() { //After changes "i", we inform the table model about new value public

What is the complexity of this simple piece of code?

删除回忆录丶 提交于 2019-11-27 01:17:58
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 a copy of sentence and run through all the letters in sentence to copy them over If you have to iterate

Create a string with n characters

﹥>﹥吖頭↗ 提交于 2019-11-27 00:23:58
Is there a way in java to create a string with a specified number of a specified character? In my case, I would need to create a string with 10 spaces. My current code is: StringBuffer outputBuffer = new StringBuffer(length); for (int i = 0; i < length; i++){ outputBuffer.append(" "); } return outputBuffer.toString(); Is there a better way to accomplish the same thing. In particular I'd like something that is fast (in terms of execution). The for loop will be optimized by the compiler. In such cases like yours you don't need to care about optimization on your own. Trust the compiler. :) Edit: