C# Tutorial for Beginner : Chapter 7 Single-Dimension Array
Introduction
An array keeps a settled size consecutive group of elements of a similar type. An array is utilized to keep a group of information, however it is regularly more valuable to think about an array as an accumulation of variables of a similar type put away at bordering memory areas. Rather than pronouncing singular variable which somehow make your code become messy, for example, value0, value1, ..., until value100, you proclaim one array variable, for example, numbers and utilize value[0], value[1], and ..., value[100] to speak to singular factors. A particular element in an array is gotten to by a record.All arrays comprise of bordering memory areas. The most minimal deliver relates to the first element and the most noteworthy deliver to the last element.Okay, in this post, I will explain single-dimension array. If there single then there must be double or more, right ? Of course there more other type than single-dimension. I will discuss the other in future post. Now, Let's dive this single-dimension into further step-by-step.
Declaring Arrays
To declare an array in C#, you can use the following syntax :datatype[] arrayName;
In C#, you must specify data type in all variables including array. If you have or already make a class then you can make array from them. Followed by data type is open and close bracket '[ ]'. This '[ ]' represent that a variable you make is an array. Then you give your array variable a name. If you don't give it name so how can you access them later :v.
Besides the initializing way I told you above, it is possible to initialize an array at the same time with declaration, in that case, you do not have to mention of specify array size rank because it is already supplied by the number of elements in the initialization list. For example:
Array Initialization
Declare an array does not initialize the array in the memory. At the point when the array variable is initialized, you can relegate values to the//tips to give array size if you want input your own size int n = 5; int x = Convert.ToInt32(Console.ReadLine()); Node [] nodes1 = new Node[n]; Node [] nodes2 = new Node[x]; //normal way int[] values = new int[100]; values[0]=90; values[1]=80; values[2]=1000; values[3]=98;As I told you before, you must give your array a size. If you want make a collection of data and you do not know how many elements you in your array, then I suggest you to use 'List'. In example above, you can initialize in normal way. Array elements begin from 0 to (n-1). Of course you element's size doesn't change from 100 to 99. It is just start from 0 and end until index 99 so it's size still 100. When you initialize like 'values' array, all values inside element will be automatically initialized by 0 (zero). It will different if it applied to nodes array because each nodes's elements will be initialized by null value.
Besides the initializing way I told you above, it is possible to initialize an array at the same time with declaration, in that case, you do not have to mention of specify array size rank because it is already supplied by the number of elements in the initialization list. For example:
int[] numbers1 = new int[] { 9, 8, 7, 6, 5 }; int[] numbers2 = { 9, 8, 7, 6, 5 }; string[] names1 = { "Jon", "Mark", "Ann", "Sam", "Lee", "Wulin", "Mike" }; string[] names2 = new string [] { "Jon", "Mark", "Ann", "Sam", "Lee", "Wulin", "Mike" }; int[] numbers3; numbers3 = new int[] { 9, 8, 7, 6, 5 }; //numbers3 = { 9, 8, 7, 6, 5 }; // this line will give an error
Accessing Array Elements
In my previous post about looping, I already gave an example how to access an array. The simple way to access an array's value is to get or set one or two by index just like the first example "values[1]=80". However in most of cases people often use looping to access array. Take a look example I wrote below :
You only can get element's value by using foreach loop but you can change value using for loop.
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; foreach (int number in numbers) { Console.WriteLine(n + " + " + (number) + " = " + (n + number)); } for (int i = 0; i <= numbers.GetUpperBound(0); i++) { Console.WriteLine("[" + i + "] = " + numbers2[i]); } for (int i = 0; i <= numbers.GetUpperBound(0); i++) { Console.WriteLine("[" + i + "] = " + numbers2[i]); }
You only can get element's value by using foreach loop but you can change value using for loop.
Example Code
Here another example the usage of array. this simple code is about swap the target array.
So the general idea is copy arr from latest index into swap from first index. Variable control of arr will decrease while swap is increase. For more understanding you can see the picture below.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Arrays { class Program { static void Main(string[] args) { int n = Convert.ToInt32(Console.ReadLine()); string[] arr_temp = Console.ReadLine().Split(' '); int[] arr = Array.ConvertAll(arr_temp, Int32.Parse); int[] swap = new int[arr.Length]; for (int i = 0,j=arr.Length-1; i < arr.Length;i++,j-- ) { swap[i]=arr[j]; Console.Write(arr[j]+" "); } Console.ReadLine(); for (int q = 0; q < swap.Length;q++ ) { Console.Write(swap[q] + " "); } Console.ReadLine(); } } }
So the general idea is copy arr from latest index into swap from first index. Variable control of arr will decrease while swap is increase. For more understanding you can see the picture below.
Comments
Post a Comment