stringbuilder

Difference between StringBuilder and StringBuffer

六眼飞鱼酱① 提交于 2019-12-04 03:11:24
问题 What is the main difference between StringBuffer and StringBuilder ? Is there any performance issues when deciding on any one of these? 回答1: StringBuffer is synchronized, StringBuilder is not. 回答2: StringBuilder is faster than StringBuffer because it's not synchronized . Here's a simple benchmark test: public class Main { public static void main(String[] args) { int N = 77777777; long t; { StringBuffer sb = new StringBuffer(); t = System.currentTimeMillis(); for (int i = N; i --> 0 ;) { sb

How can a StringBuilder best be converted to a String[]?

安稳与你 提交于 2019-12-04 02:53:26
The following code sort of works, but fixes the number of elements in String[]. Is there a way to make a String[] add the number of elements needed dynamically? private static StringBuilder names = new StringBuilder(); ... public String[] getNames() { int start = 0; int end = 0; int i = 0; String[] nameArray = {"","","",""}; while (-1 != end) { end = names.indexOf(TAB, start); nameArray[i++] = names.substring(start, end); start = ++end; // The next name is after the TAB } return nameArray; } So you're just trying to split on tab? How about: return names.toString().split(TAB); Note that split

Maximum number of characters stringbuilder can accommodate

╄→гoц情女王★ 提交于 2019-12-04 02:52:33
I need to write 10,000 x 30,000 characters. will a single stringbuilder be able to acomodate all characters or should I think of an array of stringbuilders? I do not have access to the test cases, so I cannot actually verify it myself. Hope I will find the answer here. Thanks in advance. EDIT: I tried to add 10000 x 30000 characters using a loop. I get the following exceptions. Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Arrays.java:2367) at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:130) at java.lang

Java 性能调优的 11 个实用技巧

南楼画角 提交于 2019-12-04 02:12:20
点击上方 " 程序员小乐 "关注, 星标或置顶 一起 成长 每天凌晨00点00分, 第一时间与你相约 每日英文 Never give up on something you really want. It’s difficult to wait, but worse to regret. 永远不要放弃你真正想要的东西。 等待虽难,但后悔更甚。 每日掏心 话 人生最可悲的是:生活把我们变成了当初最讨厌的那种人,可我们终究还是过不上自己想要的生活。 责编:乐乐 来源:iteye.com/news/32754 程序员小乐(ID:study_tech)第 698 次推文 图片来自网络 往日回顾: 感受Lambda之美,建议收藏,需要时查阅 正文 大多数开发人员理所当然地以为性能优化很复杂,需要大量的经验和知识。好吧,不能说这是完全错误的。优化应用程序以获得最佳性能不是一件容易的事情。但是,这并不意味着如果你不具备这些知识,就不能做任何事情。这里有11个易于遵循的建议和最佳实践可以帮助你创建一个性能良好的应用程序。 大部分建议是针对Java的。但也有若干建议是与语言无关的,可以应用于所有应用程序和编程语言。在讨论专门针对Java的性能调优技巧之前,让我们先来看看通用技巧。 1.在你知道必要之前不要优化 这可能是最重要的性能调整技巧之一。你应该遵循常见的最佳实践做法并尝试高效地实现用例。但是

Java-String.intern的深入研究

天涯浪子 提交于 2019-12-04 00:10:32
来源:Secondworld When---什么时候需要了解String的intern方法: 面试的时候(蜜汁尴尬)!虽然不想承认,不过面试的时候经常碰到这种高逼格的问题来考察我们是否真正理解了String的不可变性、String常量池的设计以及String.intern方法所做的事情。但其实,我们在实际的编程中也可能碰到可以利用String.intern方法来提高程序效率或者减少内存占用的情况,这个我们等下会细说。 What---String.intern方法究竟做了什么: Returns a canonical representation for the string object. A pool of strings, initially empty, is maintained privately by the class String. When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is

转:String为值类型还是引用类型

萝らか妹 提交于 2019-12-03 23:03:39
关于String为值类型还是引用类型的讨论一直没有平息,最近一直在研究性能方面的问题,今天再次将此问题进行一次明确。希望能给大家带来点帮助。 如果有错误请指出。 来看下面例子: 复制代码 复制代码 //值类型 int a = 1; int b = a; a = 2; Console.WriteLine("a is {0},b is {1}", a, b); //字符串 string str1 = "ab"; string str2 = str1; str1 = "abc"; Console.WriteLine("str1 is {0},str2 is {1}", str1, str2); Console.Read(); 复制代码 复制代码 根据上面的例子:你觉得输出结果应该是什么? 输出结果: //结果: //a is 2,b is 1 //str1 is abc,str2 is ab str2依然是ab,并没有随str1的改变而改变。 如果string是引用类型,按理Str1和Str指针都指向同一内存地址,如果Str的内容发生改变,Str1应该也会相应变化。 此例子,看着string更像是值类型。 但是MSDN却说String是引用类型, 引用类型包括: String 所有数组,即使其元素是值类型 类类型,如 Form 委托 查看具体引用是否相同

How to check null on StringBuilder?

淺唱寂寞╮ 提交于 2019-12-03 23:01:46
I want to check for null or empty specifically in my code. Does empty and null are same for StringBuilder in Java? For example: StringBuilder state = new StringBuilder(); StringBuilder err= new StringBuilder(); success = executeCommand(cmd, state, err); /* here executeCommand() returns empty or null in state, I cant make changes in <br/> executeCommand() so can I check it in my code somehow for state, if its null or empty? */<br/> if (state == null) { //do blabla1 } if (state.tostring().equals("")) { //do blabla2 } Does above code make sense or how should I change it? Kick No, null and empty

.NET StringBuilder preappend a line

狂风中的少年 提交于 2019-12-03 22:42:41
I know that the System.Text.StringBuilder in .NET has an AppendLine() method, however, I need to pre-append a line to the beginning of a StringBuilder . I know that you can use Insert() to append a string, but I can't seem to do that with a line, is there a next line character I can use? I am using VB.NET, so answers in that are preferable, but answers in C# are ok as well. is there a next line character I can use? You can use Environment.NewLine Gets the newline string defined for this environment. For example: StringBuilder sb = new StringBuilder(); sb.AppendLine("bla bla bla.."); sb.Insert

How much to grow buffer in a StringBuilder-like C module?

半城伤御伤魂 提交于 2019-12-03 22:40:50
In C, I'm working on a "class" that manages a byte buffer, allowing arbitrary data to be appended to the end. I'm now looking into automatic resizing as the underlying array fills up using calls to realloc . This should make sense to anyone who's ever used Java or C# StringBuilder . I understand how to go about the resizing. But does anyone have any suggestions, with rationale provided, on how much to grow the buffer with each resize? Obviously, there's a trade off to be made between wasted space and excessive realloc calls (which could lead to excessive copying). I've seen some tutorials

Equivalent of StringBuilder for byte arrays

心不动则不痛 提交于 2019-12-03 22:05:50
This is a simple one, and one that I thought would have been answered. I did try to find an answer on here, but didn't come up with anything - so apologies if there is something I have missed. Anyway, is there an equivalent of StringBuilder but for byte arrays? I'm not bothered about all the different overloads of Append() - but I'd like to see Append(byte) and Append(byte[]) . Is there anything around or is it roll-your-own time? Would MemoryStream work for you? The interface might not be quite as convenient, but it offers a simple way to append bytes, and when you are done you can get the