mutable

Are strings mutable in Ruby?

若如初见. 提交于 2019-11-26 16:39:26
问题 Are Strings mutable in Ruby? According to the documentation doing str = "hello" str = str + " world" creates a new string object with the value "hello world" but when we do str = "hello" str << " world" It does not mention that it creates a new object, so does it mutate the str object, which will now have the value "hello world" ? 回答1: Yes, << mutates the same object, and + creates a new one. Demonstration: irb(main):011:0> str = "hello" => "hello" irb(main):012:0> str.object_id => 22269036

Generating sublists using multiplication ( * ) unexpected behavior [duplicate]

时间秒杀一切 提交于 2019-11-26 15:27:10
This question already has an answer here: List of lists changes reflected across sublists unexpectedly 12 answers Nested List Indices [duplicate] 2 answers I'm sure this has been answered somewhere but I wasn't sure how to describe it. Let's say I want to create a list containing 3 empty lists, like so: lst = [[], [], []] I thought I was being all clever by doing this: lst = [[]] * 3 But I discovered, after debugging some weird behavior, that this caused an append update to one sublist, say lst[0].append(3) , to update the entire list, making it [[3], [3], [3]] rather than [[3], [], []] .

What is difference between mutable and immutable String in java

旧时模样 提交于 2019-11-26 15:23:40
问题 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. 回答1: Case 1: String str = "Good"; str = str + "

Why are integers immutable in Python? [duplicate]

☆樱花仙子☆ 提交于 2019-11-26 14:22:12
问题 This question already has an answer here: Immutable vs Mutable types 16 answers I understand the differences between mutable and immutable objects in Python. I have read many posts discussing the differences. However, I have not read anything regarding WHY integers are immutable objects. Does there exist a reason for this? Or is the answer "that's just how it is"? Edit: I am getting prompted to 'differentiate' this question from other questions as it seems to be a previously asked question.

Does Java have mutable types for Integer, Float, Double, Long?

空扰寡人 提交于 2019-11-26 14:18:19
问题 I am in a situation where I want to use mutable versions of things like Integer. Do I have to use these classes (below) or does Java have something built in? http://www.java2s.com/Code/Java/Data-Type/Amutableintwrapper.htm 回答1: No, Java doesn't have these built in. And that is for a reason. Using mutable types is dangerous, as they can easily be misused. Additionally, it is really easy to implement it. For example, commons-lang has a MutableInt. 回答2: You could always wrap the value in an

Correct Style for Python functions that mutate the argument

♀尐吖头ヾ 提交于 2019-11-26 14:16:08
问题 I would like to write a Python function that mutates one of the arguments (which is a list, ie, mutable). Something like this: def change(array): array.append(4) change(array) I'm more familiar with passing by value than Python's setup (whatever you decide to call it). So I would usually write such a function like this: def change(array): array.append(4) return array array = change(array) Here's my confusion. Since I can just mutate the argument, the second method would seem redundant. But

Immutable/Mutable Collections in Swift

自作多情 提交于 2019-11-26 11:58:10
问题 I was referring to Apple\'s Swift programming guide for understanding creation of Mutable/ immutable objects(Array, Dictionary, Sets, Data) in Swift language. But I could\'t understand how to create a immutable collections in Swift. I would like to see the equivalents in Swift for the following in Objective-C Immutable Array NSArray *imArray = [[NSArray alloc]initWithObjects:@\"First\",@\"Second\",@\"Third\",nil]; Mutable Array NSMutableArray *mArray = [[NSMutableArray alloc]initWithObjects:@

F#: let mutable vs. ref

≯℡__Kan透↙ 提交于 2019-11-26 10:25:52
问题 First, I acknowledge the possibility that this question could be a duplicate; just let me know. I\'m curious what the general \"best practice\" is for those situations when mutability is desired. F# seems to offer two facilities for this: the let mutable binding, which seems to work like variables in \"most\" languages, and the reference cell (created with the ref function) that requires explicit dereferencing to use. There are a couple of cases where one is \"forced\" into one or the other:

Is Integer Immutable

≯℡__Kan透↙ 提交于 2019-11-26 05:19:40
问题 I know this is probably very stupid, but a lot of places claim that the Integer class in Java is immutable, yet the following code: Integer a=3; Integer b=3; a+=b; System.out.println(a); Executes without any trouble giving the (expected) result 6. So effectively the value of a has changed. Doesn\'t that mean Integer is mutable? Secondary question and a little offtopic: \"Immutable classes do not need copy constructors\". Anyone care to explain why? 回答1: Immutable does not mean that a can

a mutable type inside an immutable container

假如想象 提交于 2019-11-26 04:54:19
问题 I\'m a bit confused about modifying tuple members. The following doesn\'t work: >>> thing = ([\'a\'],) >>> thing[0] = [\'b\'] TypeError: \'tuple\' object does not support item assignment >>> thing ([\'a\'],) But this does work: >>> thing[0][0] = \'b\' >>> thing ([\'b\'],) Also works: >>> thing[0].append(\'c\') >>> thing ([\'b\', \'c\'],) Doesn\'t work, and works (huh?!): >>> thing[0] += \'d\' TypeError: \'tuple\' object does not support item assignment >>> thing ([\'b\', \'c\', \'d\'],)