Tuesday 28 August 2018

Arrays


One-dimensional Arrays:
A list of items can be given one variable name using only one subscript and such a variable is called a single-subscripted variable or  a one dimensional array.
            Ex: x[0],x[1],x[2],…………, x[n]
The subscript can begin with 0 or 1
Like any other variable arrays must be declared and created in the computer memory before they are used. Creation of an array involves 3 steps:
1.                  Declare the array
2.                  Create memory locations
3.                  Put values into the memory location

Declaration of arrays:
Arrays in java may be declared in two forms:

Form1:  type arrayname[ ];

Form2:  type [ ] arrayname;

Examples:
int                    number[ ];
float                 average[ ];
int[ ]                 counter;
float[ ] marks;
Remember that we do not enter the size of the arrays in declaration.

Creation of arrays:
After declaration of an array we need to create it ion memory. Java allows us to create arrays using new operator only. As shown below:
                                    arrayname= new type[size];
Example:
            number            =          new int[5];
            average            =          new float[10];

Initialization of Arrays:
The final step is to put values into the array created. This process is known as initialization this done using the array subscripts as shown below:
                        Arrayname[subscript]=value;
Example:
number[0]=35;
number[1]=40;
…………………
…………………
number[4]=19;
note: java creates arrays starting with a subscript of 0 and ends with a value one less than the size specified.
We can also initialize arrays automatically  in the same way as the ordinary variables when they are declared, as shown below:
type arrayname[  ] = { list of values };
Example:
                        int number[ ] = { 35,40,20,57,19 };

Array length

In java all arrays store the allocated size in a variable named length.
Example: we can access the length of the array A is
            int Asize=A.length;

import java.io.*;
class array1
{
public static void main(String args[]) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Float [ ]a =new Float [5];
Float x[ ]=new Float[5];
int i,j=0;
System.out.println("enter how many");
int n=Integer.parseInt(br.readLine());
System.out.println("enter the array elements");
for(i=0;i<n;i++)
{
a[i]=Float.valueOf(br.readLine());
}
System.out.println("  given array is....");
for(i=0;i<n;i++)
{
System.out.println("element at index "+ i +"   is  " +a[i]);
}
}
}

OUTPUT::
D:\ \programs>javac array1.java
D:\ \programs>java array1
enter how many
5
enter the array elements
1.1
1.2
1.3
1.4
1.5
given array is....
element at index 0   is  1.1
element at index 1   is  1.2
element at index 2   is  1.3
element at index 3   is  1.4
element at index 4   is  1.5

Two dimensional Arrays:
            For creating two-dimensional arrays we must follow the same steps as that of simple arrays. We may create a two dimensional array like this:
                        int myArray[ ][ ];
                        myArray = new int[3][4];
                                    (or)
                        int myArray[ ][ ]=new int [3][4];

The following example is for addition two matrices:
import java.io.*;
class matrix1
{
public static void main(String args[])throws IOException
{
int a[ ][ ]=new int [3][3];
int b[ ][ ]=new int [3][3];
int c[ ][ ]=new int [3][3];
int i,j;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter how many rows and columns for matrix A:");
int m=Integer.parseInt(br.readLine());
int n= Integer.parseInt(br.readLine());
System.out.println("enter the lements into matrix:A");
for( i=0;i<m;i++)
{
for( j=0;j<n;j++)
{
a[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("enter how many rows and columns for matrix B:");
int p=Integer.parseInt(br.readLine());
int q= Integer.parseInt(br.readLine());
System.out.println("enter the lements into matrix:B");
for( i=0;i<p;i++)
{
for( j=0;j<q;j++)
{
b[i][]=Integer.parseInt(br.readLine());
}
}
if(m!=p&&n!=q)
{
System.out.println("Addition is not Possible");
System.exit(0);
}
else
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
}
System.out.println("result matrix is...");
for(i=0;i<m;i++)
{
System.out.println();
for(j=0;j<n;j++)
{
System.out.print("\t"+c[i][j]);
}
}
}
}
Output:
D:\ \programs>javac matrix1.java
D:\ \programs>java matrix1
enter how many rows and columns for matrix A:
2
2
enter the lements into matrix:A
1
2
3
4
enter how many rows and columns for matrix B:
2
2
enter the lements into matrix:B
1
2
3
4
result matrix is...

        2       4
        6       8

No comments: