array-initialization

Why passing {a, b, c} to a method doesn't work?

你说的曾经没有我的故事 提交于 2019-11-27 19:19:22
问题 I've tried to pass an initialization list {...} to a constructor and it didn't work. When I instead declared it in a method local variable (int[]) it worked flawlessly. Why is that? public class QuickSort { int[] a; public QuickSort(int[] a) { this.a = a; } public static void main(String[] args) { // ################### // ### WORKS ## // ################### int[] a = {8,12,79,12,50,44,8,0,7,289,1}; QuickSort sort = new QuickSort(a); // ################### // ### DOESN'T WORK ## // ##########

How can I declare a two dimensional string array?

梦想的初衷 提交于 2019-11-27 18:54:47
string[][] Tablero = new string[3][3]; I need to have a 3x3 array arrangement to save information to. How do I declare this in C#? explorer string[,] Tablero = new string[3,3]; You can also instantiate it in the same line with array initializer syntax as follows: string[,] Tablero = new string[3, 3] {{"a","b","c"}, {"d","e","f"}, {"g","h","i"} }; You probably want this: string[,] Tablero = new string[3,3]; This will create you a matrix-like array where all rows have the same length. The array in your sample is a so-called jagged array , i.e. an array of arrays where the elements can be of

Is there a way to not have to initialize arrays twice?

≡放荡痞女 提交于 2019-11-27 15:07:46
I need to initialize each element of an array to a non-constant expression. Can I do that without having to first initialize each element of the array to some meaningless expression? Here's an example of what I'd like to be able to do: fn foo(xs: &[i32; 1000]) { let mut ys: [i32; 1000]; for (x, y) in xs.iter().zip(ys.iter_mut()) { *y = *x / 3; } // ... } This code gives the compile-time error: error[E0381]: borrow of possibly uninitialized variable: `ys` --> src/lib.rs:4:33 | 4 | for (x, y) in xs.iter().zip(ys.iter_mut()) { | ^^ use of possibly uninitialized `ys` To fix the problem, I need to

Braces around string literal in char array declaration valid? (e.g. char s[] = {“Hello World”})

拈花ヽ惹草 提交于 2019-11-27 13:51:13
By accident I found that the line char s[] = {"Hello World"}; is properly compiled and seems to be treated the same as char s[] = "Hello World"; . Isn't the first ( {"Hello World"} ) an array containing one element that is an array of char, so the declaration for s should read char *s[] ? In fact if I change it to char *s[] = {"Hello World"}; the compiler accepts it as well, as expected. Searching for an answer, the only place I found which mentioned this is this one but there is no citing of the standard. So my question is, why the line char s[] = {"Hello World"}; is compiled although the

Why is this array having all remaining values initialized to zero?

岁酱吖の 提交于 2019-11-27 09:46:30
Hello I am a beginner in C programming language , recently i started learning arrays , I have studied that by default all values in an int array are garbage . Then why i am getting different values in these two cases. Case-1 int arr[5]; in this case from arr[0] till arr[4] we will have garbage values, but in next case. Case-2 int arr[5] = {1}; in this case arr[0] will have a value 1 and remaining from arr[1] to arr[4] will have value 0. My question is that, When in case-1 each un-initilized array locations are having garbage valeus then why in case-2 remaining un-initilized array locations are

Array concatenation in C#

空扰寡人 提交于 2019-11-27 08:00:15
问题 How do I smartly initialize an Array with two (or more) other arrays in C#? double[] d1 = new double[5]; double[] d2 = new double[3]; double[] dTotal = new double[8]; // I need this to be {d1 then d2} Another question: How do I concatenate C# arrays efficiently? 回答1: You could use CopyTo: double[] d1 = new double[5]; double[] d2 = new double[3]; double[] dTotal = new double[d1.Length + d2.Length]; d1.CopyTo(dTotal, 0); d2.CopyTo(dTotal, d1.Length); 回答2: var dTotal = d1.Concat(d2).ToArray();

How to initialize an array of objects?

纵然是瞬间 提交于 2019-11-27 06:13:55
问题 I just looked at this SO Post: However, the Columbia professor's notes does it the way below. See page 9. Foo foos = new Foo[12] ; Which way is correct? They seem to say different things. Particularly, in the notes version there isn't [] . 回答1: This simply won't compile in Java (because you're assigning a value of an array type to a variable of a the non-array type Foo ): Foo foos = new Foo[12]; it's rejected by javac with the following error (See also: http://ideone.com/0jh9YE): test.java:5:

How to create an array when the size is a variable not a constant?

纵饮孤独 提交于 2019-11-26 20:44:34
I've got a method that receives a variable int. That variable constitutes an array size (please, don't offer me a vector). Thus, I need to init a const int inside my method to initialize an array of specific size. Question: how do I do that? void foo(int variable_int){ int a[variable_int] = {0}; //error } int *a = new int[variable_int]; Remember to delete[] the allocated space when you are done with it! You asked for a non-vector solution but let's go over it because you might have rejected it for the wrong reasons. You shouldn't worry about performance because at the hands of any competent

How can I declare a two dimensional string array?

女生的网名这么多〃 提交于 2019-11-26 19:32:26
问题 string[][] Tablero = new string[3][3]; I need to have a 3x3 array arrangement to save information to. How do I declare this in C#? 回答1: string[,] Tablero = new string[3,3]; You can also instantiate it in the same line with array initializer syntax as follows: string[,] Tablero = new string[3, 3] {{"a","b","c"}, {"d","e","f"}, {"g","h","i"} }; 回答2: You probably want this: string[,] Tablero = new string[3,3]; This will create you a matrix-like array where all rows have the same length. The

Is there a way to not have to initialize arrays twice?

懵懂的女人 提交于 2019-11-26 18:28:37
问题 I need to initialize each element of an array to a non-constant expression. Can I do that without having to first initialize each element of the array to some meaningless expression? Here's an example of what I'd like to be able to do: fn foo(xs: &[i32; 1000]) { let mut ys: [i32; 1000]; for (x, y) in xs.iter().zip(ys.iter_mut()) { *y = *x / 3; } // ... } This code gives the compile-time error: error[E0381]: borrow of possibly uninitialized variable: `ys` --> src/lib.rs:4:33 | 4 | for (x, y)