This may be a very simple method - I start with C and need to add values to the array, for example:
int[] terms; for(int runs = 0; runs < 400; runs++) { terms[] = runs; }
For those who use PHP, this is what I'm trying to do in C:
$arr = array(); for ($i = 0; $i < 10; $i++) { $arr[] = $i; }
#1 building
int[] terms = new int[10]; //create 10 empty index in array terms //fill value = 400 for every index (run) in the array //terms.Length is the total length of the array, it is equal to 10 in this case for (int run = 0; run < terms.Length; run++) { terms[run] = 400; } //print value from each of the index for (int run = 0; run < terms.Length; run++) { Console.WriteLine("Value in index {0}:\t{1}",run, terms[run]); } Console.ReadLine();
* output:
Value in index 0: 400
Index value 1:400
Value in index 2: 400
Value in index 3: 400
Value of index 4: 400
Value of index 5: 400
Value in index 6: 400
Value in index 7: 400
Value in index 8: 400
Value of index 9: 400
* /
#2 building
You must first assign an array:
int [] terms = new int[400]; // allocate an array of 400 ints for(int runs = 0; runs < terms.Length; runs++) // Use Length property rather than the 400 magic number again { terms[runs] = value; }
#3 building
int[] terms = new int[400]; for(int runs = 0; runs < 400; runs++) { terms[runs] = value; }
#4 building
int ArraySize = 400; int[] terms = new int[ArraySize]; for(int runs = 0; runs < ArraySize; runs++) { terms[runs] = runs; }
That's how I write code.
#5 building
You cannot simply add elements to an array. You can set the element to the given position for dropping the 888 overview, but I recommend using list < int > or collection < int > instead, if you need to convert it to an array, use ToArray().
#6 building
You can do this-
int[] terms = new int[400]; for (int runs = 0; runs < 400; runs++) { terms[runs] = value; }
In addition, you can use lists - the advantage of lists is that you don't need to know the array size when instantiating lists.
List<int> termsList = new List<int>(); for (int runs = 0; runs < 400; runs++) { termsList.Add(value); } // You can convert it back to an array if you would like to int[] terms = termsList.ToArray();
#7 building
C arrays are fixed length and are always indexed. Solutions with Motti:
int [] terms = new int[400]; for(int runs = 0; runs < 400; runs++) { terms[runs] = value; }
Note that this array is a dense array, a 400 byte contiguous block in which you can place things. If you want to dynamically resize an array, use list < int >.
List<int> terms = new List<int>(); for(int runs = 0; runs < 400; runs ++) { terms.Add(runs); }
Neither int [] nor list < int > are associative arrays - in C it will be dictionary < >. Arrays and lists are dense.
#8 building
Here is the answer to how to use arrays.
However, C has a very convenient thing called System.Collections:)
Collections are ideal for using arrays, although many of them use arrays internally.
For example, C has a collection called List, which functions very similar to PHP arrays.
using System.Collections.Generic; // Create a List, and it can only contain integers. List<int> list = new List<int>(); for (int i = 0; i < 400; i++) { list.Add(i); }
#9 building
If you use C 3 to write code, you can use single line code:
int[] terms = Enumerable.Range(0, 400).ToArray();
This code snippet assumes that you have a using directive for System.Linq at the top of the file.
On the other hand, if you're looking for something that can be dynamically resized (PHP seems to be the case (I've never really learned that)), then you might want to use List instead of int []. The code is as follows:
List<int> terms = Enumerable.Range(0, 400).ToList();
Note, however, that you cannot simply Add the 401st element by setting term [400] to a value. Instead, you need to call Add () like this:
terms.Add(1337);
#10 building
static void Main(string[] args) { int[] arrayname = new int[5];/*arrayname is an array of 5 integer [5] mean in array [0],[1],[2],[3],[4],[5] because array starts with zero*/ int i, j; /*initialize elements of array arrayname*/ for (i = 0; i < 5; i++) { arrayname[i] = i + 100; } /*output each array element value*/ for (j = 0; j < 5; j++) { Console.WriteLine("Element and output value [{0}]={1}",j,arrayname[j]); } Console.ReadKey();/*Obtains the next character or function key pressed by the user. The pressed key is displayed in the console window.*/ }
#11 building
/*arrayname is an array of 5 integer*/ int[] arrayname = new int[5]; int i, j; /*initialize elements of array arrayname*/ for (i = 0; i < 5; i++) { arrayname[i] = i + 100; }
#12 building
It's just a different approach:
int runs = 0; bool batting = true; string scorecard; while (batting = runs < 400) scorecard += "!" + runs++; return scorecard.Split("!");
#13 building
If you do need arrays, the following may be the easiest:
using System.Collections.Generic; // Create a List, and it can only contain integers. List<int> list = new List<int>(); for (int i = 0; i < 400; i++) { list.Add(i); } int [] terms = list.ToArray();
#14 building
If you don't know the size of the array, or you already have an existing array to Add. You can solve this problem in two ways. The first is to use a generic list < T >: to do this, you need to convert the array to var termsList = terms.ToList(); and use the Add method. After that, use var terms = termsList.ToArray(); to convert back to the array.
var terms = default(int[]); var termsList = terms == null ? new List<int>() : terms.ToList(); for(var i = 0; i < 400; i++) termsList.Add(i); terms = termsList.ToArray();
The second method is to resize the current array:
var terms = default(int[]); for(var i = 0; i < 400; i++) { if(terms == null) terms = new int[1]; else Array.Resize<int>(ref terms, terms.Length + 1); terms[terms.Length - 1] = i; }
If you are using. NET 3.5 Array.Add(...);
Both methods allow you to operate dynamically. If you want to add many items, just use list < T >. If there are only a few items, then resizing the array will have better performance. This is because creating list < T > objects requires more effort.
Times tick:
3 items
Array resizing time: 6
List added: 16
400 project
Array resizing time: 305
List added: 20
#15 building
As others have described, using List as a mediation is the easiest way, but because your input is an array and you don't just want to keep the data in a List, I think you might be worried about performance.
The most efficient method might be to allocate a new array and then use Array.Copy or Array.CopyTo. If you just want to add items at the end of the list, this is not difficult:
public static T[] Add<T>(this T[] target, T item) { if (target == null) { //TODO: Return null or throw ArgumentNullException; } T[] result = new T[target.Length + 1]; target.CopyTo(result, 0); result[target.Length] = item; return result; }
If necessary, I can also publish code for the Insert extension method that uses the target index as input. A little more complicated, and use the static method Array.Copy 1-2 times.
#16 building
Thracx based answer (I don't have enough points to answer):
public static T[] Add<T>(this T[] target, params T[] items) { // Validate the parameters if (target == null) { target = new T[] { }; } if (items== null) { items = new T[] { }; } // Join the arrays T[] result = new T[target.Length + items.Length]; target.CopyTo(result, 0); items.CopyTo(result, target.Length); return result; }
This allows you to add more than one item to the array, or simply pass the array as a parameter to connect the two arrays.
#17 building
Use Linq Method Concat Can simplify this process
int[] array = new int[] { 3, 4 }; array = array.Concat(new int[] { 2 }).ToArray();
Result 3,4,2
#18 building
I'll add it as another variation. I prefer this type of functional Codeline.
Enumerable.Range(0, 400).Select(x => x).ToArray();