Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Im trying to do exactly what the title says
int[] weeks = {};
weeks[weeks.Length]=1;
this doesnt work. There is also no .Add Method.
Any ideas? or is this not possible
Line 1 declares and initializes a new 1-dimensional array of int without specific size.
Line 2 resizes the array (weeks) to its actual size, plus 1.
Line 3 assigns the value 1 to the element at the last position (that we created in Line 2)
Remember: int[5] weeks; -> to access last element you have to use index 4 -> weeks[4] = 1
int[] weeks = {};
Array.Resize(ref weeks,weeks.Length + 1);
weeks[weeks.Length - 1]=1;
Arrays in C# by definition are of a fixed size, and cannot be dynamically expanded.
I would suggest using a List<>, as they can be dynamically expanded, much like vectors in other programming languages.
List<int> weeks = new List<int>();
weeks.add(1);
See more here.
First of all there is no such thing razor array. Razor is a view engine and array is a data structure.
Arrays have fixed length so if you declare an array with the length of 5:
int[] weeks = new int[5];
Trying to add an element to a fifth place will result in IndexOutOfRangeException
If you need some data-structure with variable size you could look at all the objects that implement IList interface for example a List, an ArrayList and others.
IList interface also defines Add method that you requested.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
Lets say I have a linked list that's 3 nodes long. And the list is:
1-3-4
What it the simplest and the most efficient algorithm there is the get a new node (lets say - 5), and implement him as the first node, so the linked list will be
5-1-3-4
Thank you!
I'm quite new to the subject of Linked List, so I haven't really tried anything.
If you use standard LinkedList from System.Collections.Generic then you can use AddFirst:
LinkedList<int> myList = new LinkedList<int>();
myList.AddRangeAfter(new int[] { 1, 3, 4 });
// Let's have a look a list before adding 5
Console.WriteLine("Before: " + string.Join("-", myList));
// Let's add 5 as a first item:
myList.AddFirst(5);
// Let's have a look at the list now
Console.WriteLine("After: " + string.Join("-", myList));
Output:
Before: 1-3-4
After: 5-1-3-4
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I tried to print every pair of element while it's factorial
language: language-c
#include<studio library>//
in main function I try to store 2D array which size is
a[1][3];
And, I write these code
integer a[1][3] = {1,2,3,4,5};
and I find factorial of 5 = 120
I have to print three element together
it is possible 120 times;
so ,I can try 120 different value using print function function
in a[1][3];
like this output
1,2,3 is 1.
1,2,4 is 2.
1,2,5 is 3.
1,3,4 is 4.
1,3,5 up to 120
I want to know that how to store value in 2D array
but the array is a[1][3]
You may set the values of j and k to be "i-dependent" but they are not updating their value, you should add a j++,k++ next to the i++
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
The greatest of two numbers without any conditional statements. I need to get two numbers from the user and find out the greatest of them but I can't figure it out.
There are many ways to solve this but I would solve this by sorting numbers in a descending way. Try this:
int[] numbers = { 10, 20};
Array.Sort(numbers);
Array.Reverse(numbers);
Console.WriteLine("The highest number is: " + numbers[0]);
Or, just use numbers.Max() instead of Array.Sort(numbers) and Array.Reverse(numbers). There are still more than 20 ways to solve this problem.
You can use Math.Max, but it is a wrapper around a ternary statement that looks like this:
return (val1>=val2)?val1:val2;
There is a different way...
var array = new double[] { userValue1, userValue2 };
var largest = array.Max();
Or you can sort the array in descending order and get the first element... Really though all programming boils down to a conditional somewhere, and .Max() is no different, it just sorts the array and gets the biggest value.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can I re-initialize an array?
public int[] numbers;
In my case numbers will be set with an unknown-length array. For example, when I use it first, it will have 8 elements, next time only 5, then 12.
I read some about this topic, and found only one relevant:
http://social.msdn.microsoft.com/Forums/en-US/bee99ac8-4ade-40ac-aa78-8d14d1d7bf9f/c-reinitialize-arrays?forum=csharplanguage
Even if I do not care about the memory usage of redeclaration (uhh), I can't make it this way, since I'm going to use the array in a timer's thick method, and gonna use the same array, until the timer stops. Then call the function again, which would put the elements into the array, then start the timer again.
Edit:
There are 3 functions in my case and the array declare as a public variable. The first function is called doAction(), which calls another function which returns an array with unknown length. Then, doAction starts the timer1 (thick method), which needs to reach the array from doAction. That's the reason why I used a global variable to put the array in.
Of course you can do it that way. You cannot change the size of an array. When you stop the Timer, simply create a new array and assign it to the same variable. It's the variable that matters. You'll be getting the array from the variable in the Tick event handler so you'll always get the current array.
When you use it first, do
numbers = new int[5];
when you use it again, do
numbers = new int[12];
If it's not that simple, then you haven't explained your problem very well.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I am starting to use the Numpy and really like it's array handling capabilities. Is there some library that I can use in C# that provides similar capabilities with arrays. The features I would like most are:
Creating one array from another
Easy/trival iteration of arrays of n dimension
Slicing of arrays
NumPY has been ported to .NET via IronPython.
I don't think you need a library. I think LINQ does all you mention quite well.
Creating one array from another
int[,] parts = new int[2,3];
int[] flatArray = parts.ToArray();
// Copying the array with the same dimensions can easily be put into an extension
// method if you need it, nothing to grab a library for ...
Easy iteration
int[,] parts = new int[2,3];
foreach(var item in parts)
Console.WriteLine(item);
Slicing of arrays
int[] arr = new int[] { 2,3,4,5,6 };
int[] slice = arr.Skip(2).Take(2).ToArray();
// Multidimensional slice
int[,] parts = new int[2,3];
int[] slice = arr.Cast<int>().Skip(2).Take(2).ToArray();
The awkward .Cast<int> in the last example is due to the quirk that multidimensional arrays in C# are only IEnumerable and not IEnumerable<T>.