Arrays - Java

An array is collection of variables of the same type arranged in the memory in an uniform order .

Types of arrays


Syntax:
Data type array name[ ];
Array name = new type [size];
Datatype []arrayname=new datatype[eliments]
Datatype arrayname[][]=new datatype[eliments][eliments]
Datatype arrayname[][]=new datatype[eliments][]
Datatype arrayname[]={values list}

 

WRITE A PROGRAM ON ARRAYS.

class Arrays
{
public static void main(String args[])
{
int a[ ],i;
a=new int[5];
a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;
a[4]=50;
for(i=0;i<5;i++)
System.out.println(a[i]);
}
}

WRITE A PROGRAM TO PRINT SUM OF NUMBERS USING ARRAYS.

class Array2
{
public static void main(String args[])
{
int a[ ]=new int[5];
int i,s=0;
for(i=0;i<5;i++)
a[i]=i+10;
for(i=0;i<5;i++)
{
s=s+a[i];
System.out.println(s);
}
}
}

WRITE A PROGRAM TO ASSIGN THE VALUES DIRECTLY TO ARRAY.

class Array3
{
public static void main(String args[])
{
int a[ ]={10,20,30,40};
for(int i=0;i<4;i++)
System.out.println(a[i]);
}
}

WRITE A PROGRAM ON DOUBLE DIMENSION ARRAY.

class Array4
{
public static void main(String args[])
{
int a[ ][ ]=new int[2][2];
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
a[i][j]=i+j;
}
}
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
System.out.println(a[i][j]);
}
}
}
}

 

 

For
More Explanation
&
Online Classes

Contact Us:
+919885348743