ARRAYS
ARRAYS
------------------------------------------
VARIABLE:-
---------- IT IS USED TO STORE A SINGLE DATA.
EX:-
int age =23;
Array:-
-------- It is used to store multiple data of same data type (Homogenous data)
Collection :-
-------------It is used to store the multiple data of different data types ,
where as a collection is known as Heterogenous data.
Array
------
It is a data structure in which we can store multiple data of same data types
-> It is a Homogenous in nature.
-> While creating array Object, we need to have capacity.
-> It is not groupable means(we cannot increase its size)
->Whenever if we are declaring array , we used to configure the size of an array.
->Array are string in index form.
-> An array index always starts from zero position.
-> Index ends with -1.
types of Arrays
---------------- 3 types of arrays
1.one dimensional array
2.Two dimensional array / multi dimensional array.
3.jagged Array.
Declaration of Array
--------------------
To declare a array we can use []
1.Array Declaration
------------------
syntax:-
--------
datatype[] arrayname; ----- ex:- int[] marks;
2.Array Creation
-------------------
syntax
------
Arrayname = new datatype[size]; --------- ex:- marks = new int[4];
4.Array initialization
----------------------------
syntax:-
---------
Arrayname[index] = value; --------- ex;- marks[1]=10; --- marks[2]=10;
syntax;-
--------
datatype[] arrayname = {v1,v2,v3,v3,v4,v5,v6.......vn};
ex:-
int[] marks={10,20,300,400,500,600};
=======================================================================
using System;
class Program
{
static void Main()
{
//datatype[] arrayname;
int[] age;
//Arrayname = new datatype[size];
age = new int[5];
// default values
Console.WriteLine(age[0]);
Console.WriteLine(age[1]);
Console.WriteLine(age[2]);
Console.WriteLine(age[3]);
Console.WriteLine(age[4]);
Console.WriteLine("-----------------");
//Arrayname[index] = value;
age[0] = 21;
age[1] = 22;
age[2] = 23;
age[3] = 24;
age[4] = 25;
Console.WriteLine(age[0]);
Console.WriteLine(age[1]);
Console.WriteLine(age[2]);
Console.WriteLine(age[3]);
Console.WriteLine(age[4]);
}
}
-----------------------------------------------------------------------------------
----------------------------
Advantages of an Array:-
-------------------------
->Using arrays we can store multiple values in a single variable.
-> save memory
->code reduce
DisAdvantages of an Array:-
-------------------------
-> Array are limit in size
-> We can't change the size in runtime.
->There is no predefined methods to manipulate the values in arrays.
Note:-
------
To Overcome the drawbacks of arrays in C#. collections are introduced.
using System;
class Program
{
static void Main()
{
int[] a = new int[5];
//adding the values to an variable name
a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;
Console.WriteLine(a[0]);//output -> 10
Console.WriteLine(a[1]);//output -> 20
Console.WriteLine(a[2]);//output -> 30
Console.WriteLine(a[3]);//output -> 40
Console.WriteLine(a[4]);//output -> 0
}
}
-----------------------------------------------------------------------------------
-----------------------------
using System;
class Program
{
static void Main()
{
int[] age = new int[4];
string[] name = new string[4];
age[0] = 21;
age[1] = 22;
age[2] = 23;
age[3] = 24;
name[0] = "bhavana";
name[1] = "laxmi";
name[2] = "bharathi";
name[3] = "anusha";
Console.WriteLine("my name is :" + name[0] + " "+"my age is :"+
age[0]);
Console.WriteLine("my name is :" + name[1] + " " + "my age is :" +
age[1]);
Console.WriteLine("my name is :" + name[2] + " " + "my age is :" +
age[2]);
Console.WriteLine("my name is :" + name[3] + " " + "my age is :" +
age[3]);
}
}
===================================================================================
=====================
using System;
class Program
{
static void Main()
{
//5.array initilization directly
int[] id = { 1, 2, 3, 4, 5, 6, 7 };
Console.WriteLine(id[0]);
Console.WriteLine(id[1]);
Console.WriteLine(id[2]);
Console.WriteLine(id[3]);
Console.WriteLine(id[4]);
Console.WriteLine(id[5]);
Console.WriteLine(id[6]);
}
}
===================================================================================
=====
length:-
---------
syntax"- Array.Length
-----------------------------------------------------------------------------------
---
using System;
class Array
{
static void Main(string[] args)
{
int[] a = { 3, 4, 5, 8, 6, 8, 9, 0 };
Console.WriteLine(a.Length);
-------------------------------------------------------------------------------
using System;
class Program
{
static void Main()
{
//5.array initilization directly
string[] name = { "chandu", "jaggu", "rana", "NtR", "pawan kalyan",
"laddu", " mummy", " daddy", " krishna" };
}
Console.WriteLine("----- this will print the number in order wise-----");
--------------------------------------------------------------------------
using System;
class Array
{
static void Main(string[] args)
{
// even numbers
int[] a = { 1,2,3, 4, 5, 8, 6, 8, 9, };
}
Console.WriteLine("-------------");
// sum of even numbers
int sum = 0;
----------------------------------------------------------------------------------
using System;
class Array
{
static void Main(string[] args)
{
// FINDING THE VOWELS IN THE LIST OF CHARACTERS
char[] ch = { 'M', 'H', 'A', 'E', 'S', 'H' };
for (int i = 0; i < ch.Length; i++)
{
if (ch[i] == 'A' || ch[i] == 'E' || ch[i] == 'I' || ch[i] == 'O' ||
ch[i] == 'U')
{
Console.WriteLine(ch[i]);
}
}
}
}
-----------------------------------------------------------------------------------
-------------------------------
//Finding the large number in a array
using System;
class Array
{
static void Main(string[] args)
{
//Finding the large number in a array
int large = a[0];
for (int i = 0; i < a.Length; i++)
{
if(large < a[i])
{
large=a[i];
}
}
Console.WriteLine("the large number in array:"+large);
}
}
-----------------------------------------------------------------------------------
using System;
class Array
{
static void Main(string[] args)
{
//Finding the smaller number in a array
int[] a = { 3, 4, 9, 12,56, 24, 36, 1 };
int large = a[0];
for (int i = 0; i < a.Length; i++)
{
if(large > a[i])
{
large=a[i];
}
}
Console.WriteLine("the Smaller number in array: "+large);
}
}
-----------------------------------------------------------------------------------
--
class Array
{
static void Main(string[] args)
{
char[] arr = { 'a', 'b', 'a', 'c', 'a', 'b', 'a', 'c' };
int acount = 0;
---------------------------------------------------------------
wap to print 50' zero in an array
using System;
class Array
{
static void Main(string[] args)
{
int [] a = new int [10];
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i]);
}
}
}
-----------------------------------------------------------------
sorting
--------:-
syntax:-
-------
1.datatype [ , ] arrayname = new datatype[rows, columnsa]
ex:- int [,] a = new int [2,3]
syntax:-
-------
ex:-
----
int [ , ] a = { {1,2,3} ,{4,5,6} };
=========================================================================
using System;
using System.CodeDom.Compiler;
class Array
{
static void Main(string[] args)
{
int[,] a = new int[2, 3];
// adding the values
a[0, 0] = 10;
a[0, 1] = 20;
a[0, 2] = 30;
// a[1,0] = 100;
a[1, 1] = 2;
// a[1, 2] = 300;
for (int i = 0; i < 2; i++)
{
for(int j = 0; j < 3; j++)
{
Console.Write(a[i, j]);
}
Console.WriteLine();
}
}
}