variable-assignment

Java array assignment (multiple values)

爱⌒轻易说出口 提交于 2019-11-29 22:23:30
I have a Java array defined already e.g. float[] values = new float[3]; I would like to do something like this further on in the code: values = {0.1f, 0.2f, 0.3f}; But that gives me a compile error. Is there a nicer way to define multiple values at once, rather than doing this?: values[0] = 0.1f; values[1] = 0.2f; values[2] = 0.3f; Thanks! skaffman Yes: float[] values = {0.1f, 0.2f, 0.3f}; This syntax is only permissible in an initializer. You cannot use it in an assignment, where the following is the best you can do: values = new float[3]; or values = new float[] {0.1f, 0.2f, 0.3f}; Trying to

Assignment in While Loop in Python?

只谈情不闲聊 提交于 2019-11-29 22:03:42
I just came across this piece of code while 1: line = data.readline() if not line: break #... and thought, there must be a better way to do this, than using an infinite loop with break . So I tried: while line = data.readline(): #... and, obviously, got an error. Is there any way to avoid using a break in that situation? Edit: Ideally, you'd want to avoid saying readline twice... IMHO, repeating is even worse than just a break , especially if the statement is complex. If you aren't doing anything fancier with data, like reading more lines later on, there's always: for line in data: ... do

Why does i = i + i give me 0?

与世无争的帅哥 提交于 2019-11-29 19:04:16
I have a simple program: public class Mathz { static int i = 1; public static void main(String[] args) { while (true){ i = i + i; System.out.println(i); } } } When I run this program, all I see is 0 for i in my output. I would have expected the first time round we would have i = 1 + 1 , followed by i = 2 + 2 , followed by i = 4 + 4 etc. Is this due to the fact that as soon as we try to re-declare i on the left hand-side, its value gets reset to 0 ? If anyone can point me into the finer details of this that would be great. Change the int to long and it seems to be printing numbers as expected.

Visual Basic: Variable that references another variable changes when original vairable changes

独自空忆成欢 提交于 2019-11-29 18:15:21
Example: Dim a As Integer = 1 Dim b As Integer = a Console.WriteLine(b) a = 0 Console.WriteLine(b) Output: 1 0 For some reason changing a also changes b even though I am not actually changing b. Why does this happen, and how do I get around it. Integer is a Value type so when you assign 'a' to 'b' a COPY is made. Further changes to one or the other will only affect that particular copy in its own variable: Module Module1 Sub Main() Dim a As Integer = 1 Dim b As Integer = a Console.WriteLine("Initial State:") Console.WriteLine("a = " & a) Console.WriteLine("b = " & b) a = 0 Console.WriteLine(

What does the comma in this assignment statement do?

泄露秘密 提交于 2019-11-29 16:13:15
I was looking through an interesting example script I found (at this site , last example line 124), and I'm struggling to understand what the comma after particles achieves in this line: particles, = ax.plot([], [], 'bo', ms=6) The script will hit an error if the comma is omitted, but the syntax (which seems to resemble an unpacking statement) does not make much sense to me, and a statement like a, = [2,3] fails, which seems like an argument against the unpacking theory. Any insight would be greatly appreciated. It is needed to unpack the 1-tuple (or any other length-1 sequence). Example: >>>

Ruby variable assignment in a conditional “if” modifier

試著忘記壹切 提交于 2019-11-29 15:55:07
I have a question about how the Ruby interpreter assigns variables: I use this quite often: return foo if (foo = bar.some_method) where some_method returns an object or nil. However, when I try this: return foo if (true && (foo = bar.some_method)) I get: NameError: undefined local variable or method foo for main:Object. What is the difference in evaluation between the first and second lines that causes the second line to error? Read it carefully : Another commonly confusing case is when using a modifier if: p a if a = 0.zero? Rather than printing true you receive a NameError, “undefined local

How to get column of a multidimensional array in C/C++?

↘锁芯ラ 提交于 2019-11-29 15:21:12
int matrix[9][9],*p; p=matrix[0]; this works and gives first row of matrix , but how to get first column of matrix I've tried p=matrix[][0]; ? Also I don't understand why below code gets compiler error ? int matrix[9][9],p[9]; // it looks really ugly, byt why it doesn't work ? p=matrix[0]; // compiler gives "invalid array assigment" is it because multidimensional arrays are arrays of arrays - and we should interpret matrix[i][j] as j-th element of i-th nested array ? In C/C++, multidimensional arrays are actually stored as one dimensional arrays (in the memory). Your 2D matrix is stored as a

Dynamic global variables assignment

半腔热情 提交于 2019-11-29 14:41:22
I'm new in python and I'm having many troubles in using global instruction. Here is a code example: mouse = "a" background = "b" list_ab = [mouse, background] def func (): for item in list_ab: global item # I want to modify the GLOBAL item, which is mouse # and background. item = "modified" print mouse # must be "modified" not "a" print background # must be "modified" not "b" This is the problem. How can I solve it? Your problem is that Python works not the way you think. So I will try to explain what's going on in your code, line by line. mouse = "a" Assigns the string "a" to the name mouse .

Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()

故事扮演 提交于 2019-11-29 14:06:55
问题 I was unable to find anything describing how to do this, which leads to be believe I'm not doing this in the proper idiomatic Python way. Advice on the 'proper' Python way to do this would also be appreciated. I have a bunch of variables for a datalogger I'm writing (arbitrary logging length, with a known maximum length). In MATLAB, I would initialize them all as 1-D arrays of zeros of length n, n bigger than the number of entries I would ever see, assign each individual element variable

What does assigning a literal string to an NSString with “=” actually do?

三世轮回 提交于 2019-11-29 13:32:45
What does the following line actually do? string = @"Some text"; Assuming that "string" is declared thusly in the header: NSString *string; What does the "=" actually do here? What does it do to "string"'s reference count? In particular, assuming that for some reason "string" is not otherwise assigned to, does it need to be released? Thanks! The assignment is just that. The string pointer is basically a label that points to specific address in memory. Reassignment statement would point that label to another address in memory! It doesn't change reference counting or do anything beyond that in