Thursday 25 June 2015

Java arrays

Array: - variables can store only at a time. We can’t store more than one variable at a time.

Array definition: - an array is a group of like-type variables referred by a common name.

  • Arrays of any type can be created and may have one or more dimensions. But we can’t state some int values and some float values.
  • Arrays are created on dynamic memory i.e., allocated at runtime by JVM
  • A specific element in an accessed by its index starts from zero. If an array contains 'n' elements then the starting Index is 0(zero) and last Index is n-1.

Type of Arrays:- 
Arrays are categorized into two parts

  1. Single dimensional arrays
  2. Multi dimensional arrays

Single dimension arrays:-

One dimension array represents a row or a column of elements. To create an array, you first declare an array variables of desired type.
                                      Type Var-Name[];
Type declares the basic type of the array. The base type determines the data type of each element that comprises the array.
             
Eg: int marks [];

 To replace one-dimensional array, pair of square braces [] after the array name.

The actual elements are mentioned inside the curly braces {}.

Declare the array first and then allocate memory for it by using new operator .

                                         Int marks [];

                                 Marks =new int [5];
                                 Or int marks [] =new int [5];

JVM allocate for starting 5 integer elements into the array but there are not actual. Elements stored in the array. To store the elements into the array, we use statements like these

Ex:- String name[]={“xxx”,”yyyy”,”zzzz”,”www”};

Multidimensional Arrays

The single dimensional arrays only hold one value and if you want to store more than one row of data one can use multidimensional arrays. A Multidimensional Array is an array that can store more than one row of data. For example

1. To store Data in spread sheet like as follows one can use Multidimensional Array


Multidimensional Array in spread sheet
2. We can also use the Multidimensional Array in most useful applications such as matrix operations as


Multidimensional Array in matrix representation
 In programming language it can be represented as and we used two for loops to don any operations


Example:- int a[i][j] = new a[3][3];

            Where 'i' represents the data stored in row and 'j' represents data stored in column. The following is a program that is used Multidimensional Arrays for matrix multiplication    

import java.util.Scanner;
 
public class Matrix_Multiplication {
    
    Scanner scan;
    int matrix1[][], matrix2[][], multi[][];
    int row, column;
 
    void create() {
        
        scan = new Scanner(System.in);
        
        System.out.println("Matrix Multiplication");
        
        // First Matrix Creation..
        System.out.println("\nEnter number of rows & columns");
        row = Integer.parseInt(scan.nextLine());
        column = Integer.parseInt(scan.nextLine());
        
        matrix1 = new int[row][column];
        matrix2 = new int[row][column];
        multi = new int[row][column];
 
        System.out.println("Enter the data for first matrix :");
 
        for(int i=0; i<row; i++) {
            
            for(int j=0; j<column; j++) {
                
                matrix1[i][j] = scan.nextInt();
            }
        }
        
        // Second Matrix Creation..
        System.out.println("Enter the data for second matrix :");
 
        for(int i=0; i<row; i++) {
            
            for(int j=0; j<column; j++) {
                
                matrix2[i][j] = scan.nextInt();
            }
        }
    }
    
    void display() {
        
        System.out.println("\nThe First Matrix is :");
        
        for(int i=0; i<row; i++) {
            
            for(int j=0; j<column; j++) {
                
                System.out.print("\t" + matrix1[i][j]);
            }
            System.out.println();
        }
        
        System.out.println("\n\nThe Second Matrix is :");
        
        for(int i=0; i<row; i++) {
            
            for(int j=0; j<column; j++) {
                
                System.out.print("\t" + matrix2[i][j]);
            }
            System.out.println();
        }
    }
    
    void multi() {
        
        for(int i=0; i<row; i++) {
            
            for(int j=0; j<column; j++) {
                
                multi[i][j] = matrix1[i][j] * matrix2[i][j];
            }
        }
        
        System.out.println("\n\nThe Multiplication is :");
        
        for(int i=0; i<row; i++) {
            
            for(int j=0; j<column; j++) {
                
                System.out.print("\t" + multi[i][j]);
            }
            System.out.println();
        }
    }
}
 
class MainClass {
    
    public static void main(String args[]) {
        
        Matrix_Multiplication obj = new Matrix_Multiplication();
        
        obj.create();
        obj.display();
        obj.multi();
    }
}


How to find the size of an Array in Java


An array is a collection of similar types of elements under a single name. Java provides a facility to find the size of an array. Here is the process how to find the size of an array in java.

One can find the size of an array by using arrayname.length. It will returns the size of the array as

for example:- int a[10]={ 1,2,3,4,5,6,7,8,9,10}
                   a.length: it will returns 10 i.e the integer vale gives the size of the array.

For single dimensional array it will the size of the as above, but in case of tow or dimensional it will return the number of rows.

The following program shows how one can find the size of the arrays.

Program:-



JUST IN CASE IF YOU WANT TO KNOW:-

Generally they are two types of array:- fixed length and variable length. the array length is useful very good in variable length array.

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top