dynamic-arrays

How to initialize a dynamic array in java?

 ̄綄美尐妖づ 提交于 2020-01-01 08:57:07
问题 If I have a class that needs to return an array of strings of variable dimension (and that dimension could only be determined upon running some method of the class), how do I declare the dynamic array in my class' constructor? If the question wasn't clear enough, in php we could simply declare an array of strings as $my_string_array = array(); and add elements to it by $my_string_array[] = "New value"; What is the above code equivalent then in java? 回答1: You will want to look into the java

Is it safe to type-cast TArray<X> to array of X?

江枫思渺然 提交于 2020-01-01 04:08:11
问题 Today I discovered a compiler bug (QC#108577). The following program fails to compile: program Project1; {$APPTYPE CONSOLE} procedure P(M: TArray<TArray<Integer>>); begin SetLength(M, 1, 2); end; begin end. The compiler gags on the SetLength line and says: [dcc32 Error] E2029 ')' expected but ',' found I know I could fix it like this: procedure P(M: TArray<TArray<Integer>>); var i: Integer; begin SetLength(M, 1); for i := low(M) to high(M) do SetLength(M[i], 2); end; but naturally I'm keen to

Is it safe to type-cast TArray<X> to array of X?

≯℡__Kan透↙ 提交于 2020-01-01 04:08:11
问题 Today I discovered a compiler bug (QC#108577). The following program fails to compile: program Project1; {$APPTYPE CONSOLE} procedure P(M: TArray<TArray<Integer>>); begin SetLength(M, 1, 2); end; begin end. The compiler gags on the SetLength line and says: [dcc32 Error] E2029 ')' expected but ',' found I know I could fix it like this: procedure P(M: TArray<TArray<Integer>>); var i: Integer; begin SetLength(M, 1); for i := low(M) to high(M) do SetLength(M[i], 2); end; but naturally I'm keen to

Arraylist in C not working

心不动则不痛 提交于 2019-12-30 03:26:11
问题 I am currently writing a program to implement an arraylist (or dynamic array) in C. Hmm... I think I have 70 - 80% done with it, however, I found a serious problem with my code when testing them on a couple of machines. Briefly, I inserted a group of strings( char* ) into my arraylist, and tried to get and display them after couples of operations. However, this is what I got: CHECK: 1 CHECK: 2 CHECK: ܗ¿èۗ¿ CHECK: EàEàHAÿE؉Ⱥ CHECK: 5 CHECK: 6 Unfortunately, I still cannot figure out where the

How do I declare an array when I don't know the length until run time?

青春壹個敷衍的年華 提交于 2019-12-28 03:45:28
问题 I originally had an array[1..1000] that was defined as a global variable. But now I need that to be n, not 1000 and I don't find out n until later. I know what n is before I fill the array up but I need it to be global therefore need a way to define the size of a global array at run time. Context is filling an array with a linear transformation of the bytes in a file. I don't know how big the file is until someone wants to open it and the files can be of any size. 回答1: As of Delphi 4, Delphi

Passing pointers (matrix) to a function in c [duplicate]

天涯浪子 提交于 2019-12-25 18:50:44
问题 This question already has answers here : Passing an array as an argument to a function in C (9 answers) Closed 3 years ago . I have dynamically created a matrix using calloc in the usual way: int **matrix; int dim,r; scanf("%d",&dim); matrix=(int **)calloc(dim, sizeof(int *)); for(r=0; r<dim; r++) { matrix[r]=(int *)calloc(dim, sizeof(int)); } Now if I wanted to create a function to just print the elements of this matrix, I should write something like: void stampmatrix(int **matrix, int dim)

Swapping two rows in a matrix

橙三吉。 提交于 2019-12-25 17:08:50
问题 I am having a problem in swapping two rows in a matrix that is a 2D-dynamic array. I wanted to know if there is a function to use directly or if there is none I would like to know how to make one. Thanks in advance. Here is how I made the dynamic array: int **ptrMatrix = new int*[row]; for (int i = 0; i < row; i++) ptrMatrix[i] = new int[column]; 回答1: I found the solution ... it is done simply by making a temporary variable (X) and appling the following code for (int i=0;i<columns;i++) { X

MPI C - Gather 2d Array Segments into One Global Array

旧巷老猫 提交于 2019-12-25 09:19:51
问题 I am trying to print a dynamically allocated 2d array from my master process after receiving all its components from all other processes. By components I mean subarrays, or blocks. I have made the code generic to the number of processes. The following diagram will help you see how the blocks are arranged in the complete array. Each block is handled by one process. Just for here though, let's assume that i run the program using 12 processes (natively i have 8 cores), using the command: mpiexec

Sum of subarray values

依然范特西╮ 提交于 2019-12-25 03:26:47
问题 Array ( [1~course2 20:00] => Array ( [0] => Array ( [pid] => 30 [anz_tn] => 6 ) [1] => Array ( [pid] => 30 [anz_tn] => 4 ) [2] => Array ( [pid] => 30 [anz_tn] => 5 ) ) [2~Course2 08:30] => Array ( [0] => Array ( [pid] => 30 [anz_tn] => 5 ) [1] => Array ( [pid] => 11 ) [2] => Array ( [anz_tn] => 4 ) ) .... How can I get the sum of all the "anz_tn" for each subarray? (sum of all [0]['anz_tn'],[1]['anz_tn'], etc..) I've tried to use $all[][$i]['anz_tn'] but this fails. ($all is the main array,

Array in Powershell WON'T be written as an Array

别说谁变了你拦得住时间么 提交于 2019-12-25 01:14:47
问题 No matter how I write this script, Powershel will not write my array as it should, it prints a single line array. PS C:\Windows\system32> $matriz = @( (1,2,3,4), (5,6,7,8) ) Write-Host $matriz 1 2 3 4 5 6 7 8 Neither this way: PS C:\Windows\system32> $matriz = ,@((1,2,3,4), (5,6,7,8)) Write-Host $matriz 1 2 3 4 5 6 7 8 How do I print this as a real 2-dimension matrix? And... I'm trying to populate this same array with Get-Random , but it shows the following error: Failed add the number where