stringbuffer

[每日一题]对比Java中的String、StringBuffer、StringBuilder

帅比萌擦擦* 提交于 2020-04-22 02:46:46
今天来个简单的题目,轻松一下:) 相信很多人对这个问题都不陌生,只要是个Java程序员,肯定就用过这几个类: 1、String是个不可变对象,这就意味着每次字符串拼接都是创建了新的实例 2、StringBuilder和StringBuffer都是专门用来做字符串拼接的 3、StringBuffer是线程安全的,StringBuilder是线程不安全的 4、线程安全是要付出代价的,所以StringBuffer比StringBuilder要慢一点点 OK,上面四条是不是倒背如流了?那问个具体问题: 1、以下虚构出来的三种写法哪个速度最快?哪个最差? String str = "I" + "love" + "Java" + "Python" + ... + "Golang"; String str = new StringBuilder("I").append("love").append("Java").append("Python").append(...).append("Golang").toString(); String str = new StringBuffer("I").append("love").append("Java").append("Python").append(...).append("Golang").toString(); 解答:因为都是字符串字面量

How to get proper special character from string in java?

人走茶凉 提交于 2020-02-25 02:13:07
问题 I am setting a special character in DataModel. My DataModel public class LaunchModel implements Serializable { private List<LaunchModel> testquestionList; public LaunchModel() { testquestionList=new ArrayList<LaunchModel>(); } //GETTER AND SETTER } My controller method.. private void setdefaultValues() { LaunchModel temp=new LaunchModel(); List<LaunchModel> tempList=new ArrayList<LaunchModel>(); temp.setQuestion("ΔLMN and ΔXYZ"); tempList.add(temp); this.launchModel.setTestquestionList

Null pointer access: The variable can only be null at this location

让人想犯罪 __ 提交于 2020-01-13 03:53:09
问题 for(int i=0;i<n;i++){ for(int j=0;j<26;j++){ if(str.charAt(i)== strChar.charAt(j) ) * strSet1.append(str.charAt(i)); } * strSet2.append(str.charAt(i)); } Exception: Exception in thread "main" java.lang.NullPointerException at AterSeries.main(AterSeries.java:33) why this code gives null pointer exception warning: Null pointer access: The variable strSet1 can only be null at this location Null pointer access: The variable strSet2 can only be null at this location 回答1: Are strSet1 and strSet2

JUnit to test for StringBuffers

北城以北 提交于 2020-01-06 19:31:09
问题 Assignment: Write a JUnit test assuming you have two StringBuffer references named sbOne and sbTwo and you only want it to pass if the two references point to the same StringBuffer object. I want to make sure this actually a good way to approach this assignment. I wrote the assumed method for practice: package StringBufferJUni; public class StringBufferUni { public static void main(String[] args){ } public boolean StringBuff(StringBuffer sbOne, StringBuffer sbTwo){ sbTwo = sbOne; if(sbTwo=

String Vs Stringbuffer as HashMap key

守給你的承諾、 提交于 2020-01-02 07:05:35
问题 I am trying to understand why String and Stringbuilder/StringBuffer are treated differently when used as Hashmap keys. Let me make my confusion clearer with the following illustrations: Example #1, using String: String s1 = new String("abc"); String s2 = new String("abc"); HashMap hm = new HashMap(); hm.put(s1, 1); hm.put(s2, 2); System.out.println(hm.size()); Above code snippet prints '1'. Example #2, using StringBuilder(or StringBuffer): StringBuilder sb1 = new StringBuilder("abc");

Java字符串的String、StringBuilder、StringBuffer三者特性详解

戏子无情 提交于 2019-12-30 11:22:04
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 一、不可变String类型 字符串是计算机程序设计中的,最常见行为,Java的字符串操作最主要的类是String,并且String对象是不可变的(Immutable),即对象一旦创建在内存中,那么它的内容就不再改变。虽然String类中提供很多方法看起来像是可以修改String对象,比如trim()、subString()等等,但是实际上它们并没有改变原来的字符串对象,这些方法传递的只是引用的一个拷贝,所以重新创建了一个String类型的对象,并且有了新的引用。 例如下面一段代码可以说明String的不可变特性: package date0804.demo2; public class ImmutableString { public static void main(String[] args){ String str=new String("xyz"); change(str); System.out.println(str); } public static void change(String s) { s="xml"; } } 其输出结果为 str=xyz 因此,我么可以看到每当把String对象作为方法的参数时,都会复制一份引用,而该引用所指的对象其实一直待在单一的物理位置上,从未改变过。 另外

remove html tag in android

谁说我不能喝 提交于 2019-12-30 06:56:06
问题 I have follows below XML feed: <Description> <p>Touch, tap, flip, slide! You don't just read Books, you experience it.</p> </Description> Here I have to display the description like Touch,tap,flip,slide! You don 39.just read the Books, you experience it. Here I have handled the parser like: public static String removeHTML(String htmlString) { // Remove HTML tag from java String String noHTMLString = htmlString.replaceAll("\\<.*?\\>", ""); // Remove Carriage return from java String

Strings are immutable - that means I should never use += and only StringBuffer?

天涯浪子 提交于 2019-12-30 06:01:47
问题 Strings are immutable, meaning, once they have been created they cannot be changed. So, does this mean that it would take more memory if you append things with += than if you created a StringBuffer and appended text to that? If you use +=, you would create a new 'object' each time that has to be saved in the memory, wouldn't you? 回答1: Yes, you will create a new object each time with +=. That doesn't mean it's always the wrong thing to do, however. It depends whether you want that value as a

How to add an element at the end of an array?

不问归期 提交于 2019-12-30 04:36:09
问题 I want to know how to add or append a new element to the end of an array. Is any simple way to add the element at the end? I know how to use a StringBuffer but I don't know how to use it to add an element in an array. I prefer it without an ArrayList or list. I wonder if the StringBuffer will work on integers. 回答1: You can not add an element to an array, since arrays, in Java, are fixed-length. However, you could build a new array from the existing one using Arrays.copyOf(array, size) :

What is the complexity of this simple piece of code?

ⅰ亾dé卋堺 提交于 2019-12-27 22:13:05
问题 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