import java.io.*;
class ArrayDemo
{
int i;
int arr[]={1,2,3,4,5,6,7,8,9,10};
void result()
{
System.out.println("Array details \n");
for(i=0;i<10;i++)
{
System.out.print(arr[i] +"\t");
}
}
public static void main(String args[])
{
ArrayDemo d=new ArrayDemo();
d.result();
}
}
Average an Array of Values…
Example : Average
import java.io.*;
class Average
{
public static void main(String args[])
{
double nums[]={10.1,11.2,12.3,13.4,14.5};
double result=0;
int i;
for(i=0;i<5;i++)
result=result+nums[i];
System.out.println("Average is "+result/5);
}
}
Multidimensional Array in Java…
Example : TwoDArray
import java.io.*;
class TwoDArray
{
public static void main(String args[])
{
int twoD[][]=new int[4][5];
int i,j,k=0;
for(i=0;i<4;i++)
for(j=0;j<5;j++)
{
twoD[i][j]=k;
k++;
}
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
System.out.println(twoD[i][j]+"\t");
System.out.println();
}
}
}
Ragged Arrays in Java…
Example : TwoDAgain
import java.io.*;
class TwoDAgain
{
public static void main(String args[])
{
int twoD[][]=new int[4][];
twoD[0]=new int[1];
twoD[1]=new int[4];
twoD[2]=new int[3];
twoD[3]=new int[2];
int i,j,k=0;
for(i=0;i<4;i++)
for(j=0;j<twoD[i].length;j++)
{
twoD[i][j]=k;
k++;
}
for(i=0;i<4;i++)
{
for(j=0;j<twoD[i].length;j++)
System.out.println(twoD[i][j]+" ");
System.out.println();
}
}
}