Converting Java loop to C# [duplicate] - c#

This question already has answers here:
Array Size (Length) in C#
(9 answers)
Closed 6 years ago.
how should I convert Java loop code to C#?
Java:
for (int i = 0; i < edges[index].length; i++) {
edges[index][i] = 0;
edges[i][index] = 0;
}
I'm stuck with the edges[index].length part, is there any similar method in C#?
For reference, edges is int[,] array, index is some integer.

Try using GetLength?
edges.GetLength(index);
http://msdn.microsoft.com/en-us/library/system.array.getlength.aspx

In C#, Properties and Methods should start with an uppercased letter. Try Length instead of length

Related

Converting to double is changing my number C# 14.80 - > 1480 [duplicate]

This question already has answers here:
How to convert string to double with proper cultureinfo
(7 answers)
Closed 3 years ago.
I'm reading .txt file and spliting values into array like this:
for (int i = 0; i < articleItems.Length; i++)
{
List<string> splitStrings = articleItems[i].Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToList();
splitStrings[0] = splitStrings[0].Substring(12);
}
After that I'm trying to make object from those values, and everything is fine, but strange thing are happening here...
Here is the image from debugger, there is 14.80 at the beginning and when I convert that string to decimal it becomes 1480...:
Try
Double.Parse(splitStrings[2].ToString())

I need help figuring out how to work with c# matrix [duplicate]

This question already has answers here:
What is a jagged array?
(7 answers)
Closed 5 years ago.
I'm trying to understand how to work with matrix in C#.
I used to code in Java:
int[][]arr=new int[2][5]; or
int [][]arr={{2,3,4,5},{9,3}};
When I try to create an array in C# I only success to make this way:
int[,]={{1,2,3,4},{5,6,7,8}};
This line works for me but it requires same length in every column. I also try to use GetLength() when I worked with for loops and I could get only the number of columns and the size of the second column.
Is there any way to create a matrix with any size I want like the java examples?
How can I get the size of the rows/columns?
Is there any way to create a matrix with any size I want like the java examples
The term for such matrices is jagged array. Unlike int[,], this is an array of arrays; you can create it like this:
int [][] arr= { new[] {2,3,4,5}, new[] {9,3} };
How can I get the size of the rows/columns?
Now that this is an array of arrays, you can access Length of each item in arr the same way that you do in Java:
for (var r = 0 ; r != arr.Length ; r++) {
Console.WriteLine("Row {0} has {1} columns.", r, arr[r].Length);
for (var c = 0 ; c != arr[r].Length ; c++) {
...
}
}

Indeterminate number of arguments c# [duplicate]

This question already has answers here:
Does C# support a variable number of arguments, and how?
(4 answers)
Closed 5 years ago.
I'm trying to pass a variable number of arguments here.
Codification codebook = new codification(data,"Attr1","Attr2",..."AttrnumOfAttrColumns,"Result")
and also here:
int[][] inputs = symbols.ToJagged<int>("Attr1","Arrt2",..."AttrnumOfAttrColumns);
I was trying to do it with a for loop which is not the right.Is there a way to do this?
Codification codebook = new Codification(data, for (int i = 0; i < numOfAttrColumns; i++) {return "Attr"+Convert.ToString(i) }, "Result");
According to Accord.net manual
You can put
Codification codebook = new codification(data, new string[] {
"Attr1", "Attr2", ..., "AttrnumOfAttrColumns", "Result"});

How to replace all items in an array of doubles with the same value [duplicate]

This question already has answers here:
How to populate/instantiate a C# array with a single value?
(26 answers)
Closed 6 years ago.
So lets say we have an array of doubles like this one that is later used for other stuff.
double[] myArray = new double[25];
How would I go about replacing all the values in that array with a set value?
There are flashier ways, but
for (int i = 0; i < myArray.Length; ++i){
myArray[i] = foo; /*the new value*/
}
is clear and simple.
The "flashy" way (simply create a new array):
var array = Enumerable.Repeat(value, count).ToArray();
Or
Array.ConvertAll(array, e => value);

Randomly rearrange array items c# [duplicate]

This question already has answers here:
Best way to randomize an array with .NET
(19 answers)
Closed 6 years ago.
I have a c# int array that contains numbers from 1 to 100
that means that
myArray[0] = 1;
myArray[1] = 2;
....
myArray[99] = 100;
But I want to rearrange them in this array randomly, is it possible in c# ?
Using Random and Linq, you can do it easily:
Random r = new Random();
myArray = myArray.OrderBy(x => r.Next()).ToArray();
The above provides a random sort order for each element in the array using a Random object.
You need to add using System.Linq; at the top of your file in order to use OrderBy extension method.

Categories