array help please
-
Sorry, I'm new to C# so please forgive the ignorance... I have a list, and want to make another list from the first list: int[] list1 = { 1, 3, 5, 7, 9 }; int[,] list2; for (int i = 0; i < list1.Length; i++) { list2[i] = [item from list1] , [i]; } How do i put items in list2? I'm looking for: {{1,0} {3,2} {5,3} {7,4} {9,5}} Thank you for your time.
-
Sorry, I'm new to C# so please forgive the ignorance... I have a list, and want to make another list from the first list: int[] list1 = { 1, 3, 5, 7, 9 }; int[,] list2; for (int i = 0; i < list1.Length; i++) { list2[i] = [item from list1] , [i]; } How do i put items in list2? I'm looking for: {{1,0} {3,2} {5,3} {7,4} {9,5}} Thank you for your time.
int[,] gives you a two dimensional list, not a list of pairs of numbers. int[3,3] gives you a grid of 9 values, all of them a single int. To store pairs you'd need to define a struct, or you could use a map if you wanted ( which pairs values and lets you look up one value based on the other instead of an index ). I suspect that you're just experimenting, b/c I can't see any use for your final example ( the second value is always the same as the array index, so you have access to that number all the time anyhow ). Imagine I had a tic tac toe board for a game:
| |
___________
| |
___________
| |Now - I can create a grid that's 3x3 with List[3,3] and I can use co-ordinates to look up positions in that list and track the game positions. That's the sort of thing a 2D array is used for.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
int[,] gives you a two dimensional list, not a list of pairs of numbers. int[3,3] gives you a grid of 9 values, all of them a single int. To store pairs you'd need to define a struct, or you could use a map if you wanted ( which pairs values and lets you look up one value based on the other instead of an index ). I suspect that you're just experimenting, b/c I can't see any use for your final example ( the second value is always the same as the array index, so you have access to that number all the time anyhow ). Imagine I had a tic tac toe board for a game:
| |
___________
| |
___________
| |Now - I can create a grid that's 3x3 with List[3,3] and I can use co-ordinates to look up positions in that list and track the game positions. That's the sort of thing a 2D array is used for.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
I have this list: int[] list1 = { 1, 3, 5, 7, 9 }; Using 'for', how do I create the following list using the first list? {{1,0} {3,2} {5,3} {7,4} {9,5}} Thanks.
-
The second elements of the sub-array's in your second array does not make enough sense and does not match your description. {{1,0} {3,1} {5,2} {7,3} {9,4}} would make sense and match your description.
-
I'm sorry all, my example was poor. Yes, this is what is needed. I can't figure out the syntax needed inside 'for'. Could you help me please?
-
Thank you. I want to get: {{1,0} {3,2} {5,3} {7,4} {9,5}} from an original list of: int [] list1 = { 1, 3, 5, 7, 9 }; using the increment in 'for' as the second number
Then you need to define a struct that contains those two numbers and build a list of them. struct myStruct { public int value; public int index; } will work just fine. Of course then you need to write your own code if you want to search or sort the list.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
warning: untested
int[] list1 = { 1, 3, 5, 7, 9 };
int[,] list2 = new int[5,2];for (int i = 0; i < list1.Length; i++)
{
list2[i, 0] = list1[i];
list2[i, 1] = i;
} -
Thank you. I wasn't even close & wasn't aware of 'code block' which scrambled my example. Thanks again for your patience.