stringbuilder

Java new String and new StringBuilder in heap behavior

风格不统一 提交于 2019-11-29 13:03:27
Does the String pool reside on the heap? If yes, are String literals eligible for garbage collection? When using new String("abc") , we know that it creates an object on the heap and places the String literal in the String pool. So my 2nd question is: Does new StringBuilder("abc") behave the same way as new String("abc") ? If yes, how does StringBuilder manipulate the String literal inside the String pool? You are confusing compile time, load time, and runtime. A string literal is added to the constant pool at class loading time. Just a mention of a literal anywhere in the class code is enough

Mutability of string when string doesn't change in C#

和自甴很熟 提交于 2019-11-29 12:56:38
If the string operation doesn't change the value of string, will that end up in creation of a new instance? For example, string str = "foo"; str += ""; I know the difference between string and StringBuilder in C#. Shamseer K No, a new instance will be created only when the string operation changes the value in the string variable. It can be proved using the ObjectIDGenerator class. It's worth reading this complete article for proof. using System; using System.Text; using System.Runtime.Serialization; namespace StringVsStringBuilder { class Program { static void Main(string[] args) {

DAY1_leetcode

戏子无情 提交于 2019-11-29 12:17:28
1.两数之和 1 class Solution { 2 public int[] twoSum(int[] nums, int target) { 3 Map<Integer, Integer> map = new HashMap<>(); 4 for(int i = 0; i<nums.length;i++){ 5 int tmp = target - nums[i]; 6 if(map.containsKey(tmp)){ 7 return new int[]{map.get(tmp),i}; //存在即返回 8 } 9 map.put(nums[i],i); //不存在,则加入 10 } 11 throw new IllegalArgumentException("No two sum solution"); 12 } 13 }   总结:熟悉了HashMap的使用 2. 1 class Solution { 2 public int reverse(int x) { 3 try{ 4 if(x>0){ 5 StringBuilder str = new StringBuilder().append(x); 6 return Integer.parseInt(str.reverse().toString()); 7 }else{ 8 StringBuilder str =

Newline character in StringBuilder

十年热恋 提交于 2019-11-29 10:29:46
问题 How do you append a new line(\n\r) character in StringBuilder ? 回答1: I would make use of the Environment.NewLine property. Something like: StringBuilder sb = new StringBuilder(); sb.AppendFormat("Foo{0}Bar", Environment.NewLine); string s = sb.ToString(); Or StringBuilder sb = new StringBuilder(); sb.Append("Foo"); sb.Append("Foo2"); sb.Append(Environment.NewLine); sb.Append("Bar"); string s = sb.ToString(); If you wish to have a new line after each append, you can have a look at Ben Voigt's

StringBuilder

放肆的年华 提交于 2019-11-29 09:59:15
概念 字符串缓冲区。长度可变,默认底层数组16位,超过扩容一倍。 构造方法 StringBuider() StringBuilder strBuild=new StringBuilder(); StringBuilder(str) StringBuilder strBuild=new StringBuilder(“abc”); 普通方法 append() StringBuilder strBuild=new StringBuilder(); StringBuilder strBuild2=new StringBuilder(); strBuild2=strBuild.append(“Hello”) .append(“World”);(链式编程) sout(strBuild);//HelloWorld sout(strBuild2);// HelloWorld sout(“strBuild==strBuid2”);//true 布尔型 注 :append()方法可以接收任意类型的参数,并将其对应字符串加入到StringBuider中。 (1) toString() --String转化为StringBuilder String str=new String(“abc”); StringBuilder strBuild=new StringBuilder(str); -

201871010132——张潇潇《面向对象程序设计JAVA》第二周学习总结

你离开我真会死。 提交于 2019-11-29 08:23:22
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p/11475377.html 作业学习目标 适应老师教学方式,能按照自主学习要求完成本周理论知识学习; 掌握Java Application程序结构; 掌握Java语言的数据类型与变量; 学会使用Java运算符构造各类表达式; 掌握Java Application输入输出技术; 掌握Java流程控制技术(分支、循环);(重点) 掌握Math类、String类的用法。(难点) 随笔博文正文内容: 第一部分:结合 Java 和 C 基础语法的异同比较,总结本周理论知识( 30 分) 3.1 概览 大小写敏感。 访问修饰符:程序其他部分对这段代码的访问级别。 类是构建所有 java 应用程序和 applet 的构建块。 Java 应用程序的全部内容必须放置在类中。 类命名规范:大写字母开头。多个单词首字母均大写的骆驼命名法 CamelCase 。 源代码的文件名必须与公共类的名字相同,并用 .java 作为扩展名。 Java 虚拟机规范 http://docs.oracle.com/javase/specs/jvms/se7/html Java 中所有函数都属于某个类的方法

Java基础 - StringBuilder为什么线程不安全(转)

与世无争的帅哥 提交于 2019-11-29 06:20:59
引言 周五去面试又被面试的一个问题问哑巴了 面试官:StringBuilder和StringBuffer的区别在哪? 我:StringBuilder不是线程安全的,StringBuffer是线程安全的 面试官:那StringBuilder不安全的点在哪儿? 我:。。。(哑巴了) 在这之前我只记住了StringBuilder不是线程安全的,StringBuffer是线程安全的这个结论,至于StringBuilder为什么不安全从来没有去想过。 分析 在分析设个问题之前我们要知道StringBuilder和StringBuffer的内部实现跟String类一样,都是通过一个char数组存储字符串的,不同的是String类里面的char数组是final修饰的,是不可变的,而StringBuilder和StringBuffer的char数组是可变的。 首先通过一段代码去看一下多线程操作StringBuilder对象会出现什么问题 public class StringBuilderDemo { public static void main(String[] args) throws InterruptedException { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < 10; i++){

String.Format vs “string” + “string” or StringBuilder? [duplicate]

蓝咒 提交于 2019-11-29 05:37:44
Possible Duplicates: Is String.Format as efficient as StringBuilder C# String output: format or concat? What is the performance priority and what should be the conditions to prefer each of the following: String.Format("{0}, {1}", city, state); or city + ", " + state; or StringBuilder sb = new StringBuilder(); sb.Append(city); sb.Append(", "); sb.Append(state); sb.ToString(); Compiler will optimize as much string concat as it can, so for example strings that are just broken up for line break purposes can usually be optimized into a single string literal. Concatenation with variables will get

LINQ to append to a StringBuilder from a String[]

人走茶凉 提交于 2019-11-29 04:38:47
I've got a String array that I'm wanting to add to a string builder by way of LINQ. What I'm basically trying to say is "For each item in this array, append a line to this StringBuilder". I can do this quite easily using a foreach loop however the following code doesn't seem to do anything. What am I missing? stringArray.Select(x => stringBuilder.AppendLine(x)); Where as this works: foreach(String item in stringArray) { stringBuilder.AppendLine(item); } jason If you insist on doing it in a LINQy way: StringBuilder builder = StringArray.Aggregate( new StringBuilder(), (sb, s) => sb.AppendLine(s

Appending with StringBuilder

纵然是瞬间 提交于 2019-11-29 03:58:33
I have 5 checkboxes and I have to select multiple checkboxes. I made a code like this to check check box is checked: sports=(CheckBox)findViewById(R.id.sports_btn); sports.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (sports.isChecked() == true) list.add("4"); } }); I am adding a value to an array list: list ArrayList<String> list = new ArrayList<String>(); and I am retrieving the value as string like this: StringBuilder sb = new StringBuilder(); for(int i =0; i < list.size(); i++) { for (String str: list) { sb.append(str.toString()); sb.append(","); }