IndexOutOfRange exception when giving values to a jagged array [duplicate] - c#

This question already has answers here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
(5 answers)
Closed 6 years ago.
I'm trying to compare a sensor value with the values saved in a jagged array (26 saved possible cases, 5 fingers, 3d vector for each finger)
When I'm defining the values for the array, I can set the value of gestures[0] without any problems, but as soon as I start defining gestures[1],
I get an IndexOutOfRange exception.
Also, are jagged arrays good for this purpose (array of arrays of 3d vectors)?
public class Signs
{
public static double[][][] gestures = new double[26][][];
public void Define()
{
gestures[0] = new double[5][];
gestures[0][0] = new double[3] { 0.15, 0.97, -0.21 };
gestures[0][1] = new double[3] { -0.56, -0.81, -0.18 };
//...
gestures[0][5] = new double[3] {-0.21,0.44,0.2}
gestures[1] = new double[5][]; //IndexOutOfRange exception
gestures[1][0] = new double[3] { 0.21, 0.96, -0.21 };
gestures[1][1] = new double[3] { 0.19, 0.96, 0.21 };
//...

The problem is in this line
gestures[0][5] = new double[3] {-0.21,0.44,0.2}
You defined an array with length 5 (gestures[0] = new double[5][];)
So max index is 4 (0-4)

Related

Flatten jagged array and map indexes [duplicate]

This question already has answers here:
Convert 2 dimensional array
(4 answers)
Closed 7 years ago.
Is there an elegant way to flatten a 2D array in C# (using Linq or not)?
E.g. suppose
var my2dArray = new int[][] {
new int[] {1,2,3},
new int[] {4,5,6}
};
I want to call something like
my2dArray.flatten()
which would yield
{1,2,3,4,5,6}
Any ideas?
You can use SelectMany
var flat = my2dArray.SelectMany(a => a).ToArray();
This will work with a jagged array like in your example, but not with a 2D array like
var my2dArray = new [,] { { 1, 2, 3 }, { 1, 2, 3 } };
But in that case you can iterate the values like this
foreach(var item in my2dArray)
Console.WriteLine(item);

Assign a GUID in C# [duplicate]

This question already has answers here:
All possible array initialization syntaxes
(19 answers)
Closed 2 years ago.
I have a string representation of the Guid,I'm doing some testing and I'm hard-coding some GUIDs into the code. The below way can assign Guid of single value.
Guid g = new Guid("11223344-5566-7788-99AA-BBCCDDEEFF00");
Now, My question is how i can assign values to an array list of Guid[]?
You can use the below code snippet to assign values to an array list of Guid[]
Guid[] arr = new Guid[4];
arr.SetValue(new Guid("11223344-5566-7788-99AA-BBCCDDEEFF00"), 0);
arr.SetValue(new Guid("26a285e5-5435-4daa-8d36-3186bd441cff"), 1);
arr.SetValue(new Guid("6d850084-d55a-45a9-9e83-5adf22cfbf04"), 2);
arr.SetValue(new Guid("ec43e526-51c0-41bc-b04a-2f2e72c894cc"), 3);
You can use the below code snippet to fill out your Guid array with randomly generated Guids.
Guid[] arr = new Guid[10];
for (int i = 0; i < 10; i++)
{
arr.SetValue(Guid.NewGuid(), i);
}

Set an array member inside a 2d array [duplicate]

This question already has answers here:
Can someone help me assign multiple string arrays into one 2d string array?
(3 answers)
Closed 2 years ago.
I have a 2d array group
float[,] group = new float [2,3];
My understanding is that, after this line of code I have 2 3-elements arrays in group.
{{3elements},{3elements}}
I also have member
float[] member = new float[3] {1,2,3};
I want to set the first array inside the 2d array "group" to match "member" so that I can have {{1,2,3},{}}
I tried group[0] = member;
But I got an error. What's the correct way of doing this?
You can try this
float[,] group = new float[2, 3];
float[] member = new float[3] { 1, 2, 3 };
for(int i = 0; i < member.Length; i++)
{
group[0, i] = member[i];
}

Pop values off Stack<T> [duplicate]

This question already has answers here:
Adding items to a LIST<> of objects results in duplicate Objects when using a NEW in a Loop
(2 answers)
Closed 5 years ago.
So I'm trying to create a backtracking recursive algorithm for maze generation using the stack to store the coordinates of the last spot entered in an array of ints [x,y]. When I try to store the results of .Pop off the stack, it sets my variable for the first popped value but not off of the next.
public static void Main(string[] args)
{
//Your code goes here
Stack<int[]> myStack = new Stack<int[]>();
int[] pusher = new int[] {1,2};
myStack.Push(pusher);
pusher[0] = 3;
pusher[1] = 4;
myStack.Push(pusher);
while(myStack.Count > 0){
int[] test = myStack.Pop();
for(int i = 0; i < test.Length; i++){
Console.WriteLine(test[i]);
}
}
}
Wanted result would be that the console displays 3,4,1,2. Instead I'm getting back 3,4,3,4.
You are only creating one array - pusher. You are then simply replacing the numbers 1 and 2 at the indexes 0 and 1 of this array respectively.
If you create another array and add 3 and 4 to this one, you will get the expected results:
Stack<int[]> myStack = new Stack<int[]>();
int[] pusher = new int[] { 1, 2 };
myStack.Push(pusher);
int[] second = new int[2];
second[0] = 3;
second[1] = 4;
myStack.Push(second);
while (myStack.Count > 0)
{
int[] test = myStack.Pop();
for (int i = 0; i<test.Length; i++)
{
Console.WriteLine(test[i]);
}
}
Question answered by Lasse Vågsæther Karlsen in comments on question. Moving to answer.:
You're reusing the same array instance so you're in fact pushing the same array onto the stack twice. When you store 3 and 4 into the array you overwrite the 1 and 2 already there. Push does not make a copy of the array contents, only of the array reference, therefore you push the same array twice. If you want to push a copy, use myStack.Push(pusher.ToArray()); – Lasse Vågsæther Karlsen 4 hours ago

Find the index of the highest numbers in a list [duplicate]

This question already has answers here:
What is faster in finding element with property of maximum value [duplicate]
(5 answers)
How to get the index of element in the List<T> in c#
(5 answers)
Closed 5 years ago.
I have a list of integers and I want to find out the position of the biggest integer in my list.
List<int> members = new List<int>({312, 33, 122, 3979, 8712, 88})
I tried getting the biggest number by doing int max = members.Max(); and then I tried to get its index like this int highestMember = members.FindIndex(max); but it doesn't work this way
you can try :
List<int> members = new List<int> { 312, 33, 122, 3979, 8712, 8888 };
int a = members.IndexOf(members.Max());

Categories