Friday 1 March 2013

Java Arrays


 

An Array is a group of contiguous or related data items that share a common name.  A particular value is indicated by writing a number called ‘index number’ or ‘subscript’ in square bracket after the array name.  Arrays are classified into different types.  They are
1.                  One-Dimensional Array
2.                  Two-Dimensional Array
3.                  Multi-Dimensional Array

One-Dimensional Array: One-Dimensional Array is a list of homogenious items can be given one variable name using only one subscript.  Individual value of an array is called an element.  Like an ordinary variable, arrays must be declared and created in the computer memory before they are used.  Creation of an array contains two sections.  They are 1. Declaration of the array 2. Creating memory locations.
            Syntax:            type arrayname[ ];                            // declaration
                                    arrayname = new type[size];            // allocation of memory
                                                            (or)
                                    type arrayname[ ]=new type[size];
Where,
            type defines the datatype of the variables stored in the array
            arrayname defines the name of the array
            new operator is used to allocate dynamic memory in the computer for the array
            size defines the number of memory location of the array

            Example:         int number[];
                                    number = new int[5];

            It can also be initialize arrays automatically when they are declared.  The syntax is as
            Syntax:            type arrayname[]={list of values};
Where, list of values contains number of values separated by commas and surrounded by curly braces
            Example:         type number[]={35,40,56,23};

            In Java, all arrays store the allocated size in a variable named length.  We can access the length of the array using the syntax
            Syntax:            arrayname.length;
Example 1: Write a program to read 5 numbers and print the sum of it.

class arr1
{
            public static void main(String[] args)
            {
                        int sum=0;
                        int no[]=new int[10];
                        for(int i=0;i<5;i++)
                        {
                        no[i]=Integer.parseInt(args[i]);
                        sum=sum+no[i];
                        }
                        System.out.println("Total="+sum);
            }
}

Example 2: Write a program to sort the list of values in ascending order

class arr1
{
            public static void main(String[] args)
            {
                        int temp;
                        int no[]={34,78,23,11,99};
                        for(int i=0;i<no.length-1;i++)
                        {
                        for(int j=i+1;j<no.length;j++)
                        {
                        if(no[i]>no[j])
                        {
                        temp=no[i];
                        no[i]=no[j];
                        no[j]=temp;
                        }
                        }
                        }
                        for(int i=0;i<no.length;i++)
                        System.out.println(no[i]);
            }
}


Two-Dimensional Array: A Two-Dimensional Array is a collection of homogeneous data items that share a common name using two subscripts.  It stores table of data.  This representation is very useful for matrix representations as first subscript represents the number of rows and second subscript represents the number of columns.
           
Syntax:            type arrayname[][];                           // declaration
                        arrayname = new type[size1][size2];            // allocation of memory
                                                            (or)
                                    type arrayname[ ][]=new type[size1][size2];
Where,
            type defines the datatype of the variables stored in the array
            arrayname defines the name of the array
            new operator is used to allocate dynamic memory in the computer for the array
            size1 defines the number of rows of memory location of the array
            size2 defines the number of columns of memory location of the array


            Example:         int number[][];
                                    number = new int[5][6];

            It can also be initialize arrays automatically when they are declared.  The syntax is as
            Syntax:            type arrayname[][]={list of values};
                                                            Or      
                                    type arrayname[][]={{row1 values},
                                                                         {row2 values},
                                                                         ..                       }

Lab Program 6: Java program to multiply two matrices

import javax.swing.JOptionPane;
class arr2
{
            public static void main(String[] args)
            {
            int m,n,p,q,i,j,k;
            int a[][]=new int [10][10];
            int b[][]=new int [10][10];
            int c[][]=new int [10][10];
            m=Integer.parseInt(JOptionPane.showInputDialog("Enter rows of Matrix A"));
            n=Integer.parseInt(JOptionPane.showInputDialog("Enter columns of Matrix A"));
            p=Integer.parseInt(JOptionPane.showInputDialog("Enter rows of Matrix B"));
            q=Integer.parseInt(JOptionPane.showInputDialog("Enter columns of Matrix B"));
            if(n==p)
            {
            for(i=0;i<m;i++)
            {
            for(j=0;j<n;j++)
            a[i][j]=Integer.parseInt(JOptionPane.showInputDialog("Enter a["+i+"]["+j+"]:"));
            }
            for(i=0;i<p;i++)
            {
            for(j=0;j<q;j++)
            b[i][j]=Integer.parseInt(JOptionPane.showInputDialog("Enter b["+i+"]["+j+"]:"));
            }
            for(i=0;i<m;i++)
            {
            for(j=0;j<q;j++)
            {
            c[i][j]=0;
            for(k=0;k<n;k++)
            c[i][j]=c[i][j]+a[i][k]*b[k][j];
            }
            }
            for(i=0;i<m;i++)
            {
            System.out.println();
            for(j=0;j<q;j++)
            System.out.print(" "+c[i][j]);
            }
            }
            else
            {
            JOptionPane.showMessageDialog(null,"Multiplication not possible");
            }
            }
}

Multi-Dimensional Array: Multi-Dimensional Array is a list of values shared by a common name using multi subscripts.  If the array contains three subscripts then the array is called as Three-Dimensional array and so on.
            Syntax:            type arrayname[][]…[]=new int[size1][size2]…[sizen];
            Example:         type number[][][]=new int[4][5][6];

No comments:

Post a Comment