Arrays

  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Arrays as PDF for free.

More details

  • Words: 1,199
  • Pages: 21
Array in C# Single Dimension Array //20 elements integer array a[0] to a[19] int[] a = new int[20]; //3 elements integer g array y a[0] [ ] is 1,, a[1] [ ] is 5 and a[2] [ ] is 9 (without ( new)) int [] a = {1,5,9}; 12 //3 elements integer array initialized with 0 0,1,2 int [] a = new int[3] {0, 1, 2}; //5 elements string array initialized with names string[] s = new string[5] {"Mohit", "Pankaj", "Deepak", "Raj", "Amit"}; };

Multi Dimension Array int[,] a = new int[3, 2]; // array elements will be a[0,0], a[0,1], a[1,0], a[1,1], a[2,0], a[2,1] // initialized with values int[,] a = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} }; string[,] names = new string[2, 2]; string[ ] names = new string[2, string[,] string[2 2] { {" { Mohit Mohit", "Pankaj Pankaj "}}, {{" Raj Raj", "Amit Amit "}} }; //without specifying no. of elements int[ ] numbers = new int[ int[,] int[,]] { {1 {1, 2} 2}, {3 {3, 4} 4}, {5 {5, 6} }; string[,] names = new string[,] { {" Mohit", "Pankaj "}, {" Raj", "Amit "} }; //without new int[,] numbers = { {1, 2}, {3, 4}, {5, 6} }; string[,] siblings = { {" Mohit", "Pankaj "}, {" Raj", "Amit "} };

Jagged Arrays( array of arrays )

Int [][] n = new int[5][]; int[][] n = new int[][] { new int[] {1,3,5}, new int[] {2,4,6,8,10} };

Jagged Arrays( array of arrays ) int[ ][ ] a = new int[5][ ]; a[0] = new int[5]; a[1] = new int[3]; a[2] = new int[4]; a[3] [ ] = new int[2]; [ ]; a[4] = new int[6]; 1

a[0]

2

a[1]

3

a[2]

4

a[3]

5

a[4]

Jagged Arrays( array of arrays ) int[ ][ ] a = new int[5][ ]; a[0] = new int[5]; a[1] = new int[3]; a[2] = new int[4];

a[1][1]

a[3] [ ] = new int[2]; [ ]; a[4] = new int[6]; 1

a[0]

2

a[1]

3

a[2]

4

a[3]

5

a[4]

a[4][4]

a[4]

Jagged Arrays( array of arrays ) int[ ][ ] a = new int[5][ ]; a[0] = new int[5]; a[1] = new int[3]; a[2] = new int[4]; a[3] [ ] = new int[2]; [ ]; a[4] = new int[6]; 1

a[0]

2

a[1]

3

a[2]

4

a[3]

5

a[4]

a

Array Class Array Class Array names = Array.CreateInstance( typeof(String), 2, 4 ); Properties of Array Class a.Length // total number of elements (Lets a is array of integers) a.Rank // Dimension or rank of array a.IsReadOnly // return true if array is read only else false Methods et ods of o Array ay Class C ass Array.Sort(a) // sort array ; Array.Sort(a,0,5) // sort array ; sort only 0 to 5th element of Array Array Reverse(a) //Reverse array a Array.Reverse(a) a.GetUpperBound // Gets the upper bound of the specified dimension in the Array. Int r = Array.BinarySearch(a, 1); // Search 1 in array a and return its position –1 if not found

ArrayList Class Namespace System.Collections; ArrayList can contain items of multiple type and has dynamic size ArrayList al = new ArrayList(); Properties of Array Class al. Count // total number of elements (Lets a is array of integers) al.Capacity // Gets or sets the number of elements al.IsReadOnly // return true if array is read only else false Methods of Array Class al.Add(1001) al Add(“Rahul”) al.Add( Rahul ) al.Sort(); al.IndexOf(Object a); al RemoveAt(int a); al.RemoveAt(int

ArrayList Class y Example of ArrayList using System; using System.Collections; class Test { public static void Main() { ArrayList al = new ArrayList(); int i; for(i=0; i<5; i++) al Add(i); al.Add(i); al.Add("Hello"); foreach (object o in al) Console.WriteLine(o.ToString()); C l W it Li ( T St i ()) } }

ArrayList Class y Example of ArrayList using System; using System.Collections; class Test { public static void Main() { ArrayList al = new ArrayList(); int i; for(i=0; i<5; i++) al Add(i); al.Add(i); al.Add("Hello"); foreach (object o in al) Console.WriteLine(o.ToString()); C l W it Li ( T St i ()) } }

foreach Loop p Used for Arrays or Collection Objects foreach(VariableType a in ArrarName) { ….Code… }  Example: int marks = new int[5]{9,6,2,0,3}; foreach (int i in marks) foreach (int i in marks) { Console.WriteLine(i); }

foreach Loop p int m = new int[5]{9,6,2,0,3}; foreach (int i in marks) { Console.WriteLine(i); ( ); }

i is read only

i m

0

1

2

3

4

i will have value of m[4] m[0] ii.e. m[1] m[2] m[3] e 3 9 6 2 0 Console.WriteLine(i); ()

Functions [ [Access modifier]] [[Static/NonStatic]] ReturnType yp FunctionName(Arg1Type Arg1Name, Arg2Type Arg2Name…..)

Examples public static void PrintEmpInfo(int EmpID); private string GetEmpName(int EmpID);

Functions using System; class Test { public static void Main { Console WriteLine(“Hello!”); Console.WriteLine( Hello! ); } public static void Abc { Console.WriteLine(“How are You?”); } private static void Xyz { private static void Xyz { Console.WriteLine(“Okey, Bye!”); } }

Functions using System; using System; class Test { public static void Main { Console WriteLine(“Hello!”); Console.WriteLine( Hello! ); Abc(); } public static void Abc { Console.WriteLine(“How are You?”); } p y { private static void Xyz { Console.WriteLine(“Okey, Bye!”); } }

Functions using System; class Test { public static void Main() { Console.WriteLine(“Hello!”); ( ) Abc(); } public static void Abc() { Console.WriteLine(“How are You?”); int a = Max(7 3); int a = Max(7,3); } private static int Max(int a,int b) { i t t ti i t M (i t i t b) { if(a>b) return a; else return b; } }

Lets Have some Fun

Write a Program which can tell you your future

Numerology

Add all the digits of Date of Birth and predict on the basis of DigitSum (Lucky Number) For Example DOB =12/18/1978 DigitSum = 1 + 2 + 1 + 8 + 1+ 9 + 7 + 8 = 37

=> 3 + 7 = 10 => 1+0 => 1

Predict what is the future of people having Numero 1

class zzz { public bli static t ti void id Main() M i () { DateTime Dob; Dob = DateTime.Parse(“12/18/1978”); int a,b,c,d; a = Dob.Day/10 D b D /10 + D Dob.Day%10; b D %10 b = Dob.Month/10 + Dob.Month%10; c = Dob.Year/100; d = Dob.Year%100; c = c/10 /10 + c%10; %10 d = d/10 + d%10; digitSum = a + b + c + d; digitSum = digitSum/10 + digitSum%10; di itS digitSum = digitSum/10 di itS /10 + digitSum%10; di itS %10 switch(digitSum) { case 1: p="You are a Intelligent Person"; break; 2 p="You "Y llook k lik " b k case 2: like a G Greatt P Person"; break; case 3: p="You are skeptic about most of the things"; break; case 4: p="You are an emotional person"; break; case 5: p="You are Creative and Innovative"; break; case 6: 6 p="You "Y are a wonderful d f l thinker"; thi k " break; b k case 7: p="You are a true Beliver"; break; case 8: p="You are sincere and hardworkng"; break; case 9: p="You have skill of learning things fast"; break; defa lt p "Yo are from an " break; break default:p="You any other planet "; } }

Related Documents

Arrays
November 2019 37
Arrays
May 2020 23
Arrays
November 2019 37
Arrays
November 2019 39
Arrays
November 2019 46