JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Get Best Tutorial on all major programing language

Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Array – The Basics

  • Array is a data structure of a fixed number of similar data elements
  • Array Size is fixed and cannot increase at runtime.
  • It is an objects.
  • In Array, all the elements must be of the same data type.
  • Index based Access.
  • Strting point og Array index is always zero.
  • Array is the collection of same type of entity.
  • It may be array of the int, float, long, char etc.
  • It has one name having indexes which can contain many elements.
  • It eliminates from making many varibles.
  • For example int ar[10] is making 10 variable of int data type.
  • User doesn't have tyo make 10 variables.

Syntax
data type variable name[no]

  • no is the total  no of array element

While accessing Array element the index start from 0
to one less of   array size.
Example
int ar1[10];
char grade[3];
long amount[5];sum of the array is 56


Array Intialization

Array is initialized in 2 ways
1. At the time of declaration
2. After declaration each element one by one

First way

For example int ar1[] ={1,4,5,78,99};

This creates an array named ar1 of five elements and assigns it values

Second way
Array is created with new keyword.
After creation of the Array its elements are assigned .

For example
int ar2[]=new int[5];
It is craeting an array and taking the memory of 5 int variable
But arrayy creation without new keyword doesn't take memory

ar2[0]=88;
ar2[1]=18;
ar2[2]=77;[no _of_var-1]
ar2[3]=44;
ar2[4]=33;

Anonymous Array

int ar3[]=new int[] {77,66,5,44,23};

Accessing elements
The array elements are accessed one by one by its subscript/index
Above elements  can be accessed/printed in following ways

for(int x=0;x<=4; x++)
System.out.println(ar2[x]);

In the loop you can use the length to calculate the length of the array
.
Example
class array1 {
  public static void main(String args[]) {
    int ar[] 4567};

    for (int x = 0; x < ar.length; x++) {
      System.out.println(ar[x]);
    }
  }
}
 
Output
4
5
6
7
8

Download

Example To calculate the array sum
    int sum = 0;

    for (int x = 0; x < ar1.length; x++) {
      sum = sum + ar1[x];
    }
    System.out.println("sum of the array is " + sum);
  }
}
class array2 {
  public static void main(String args[]) {
    int ar1[] 1051617};


Output
sum of the array is 56




One Dimensional Array

An array having only dimension is called ine dimension array.
Above given examples  are 1 -D array.The elements is represented
in only in one dimension.





Example take two one dimension array of 5 elments. Calcualte the sum in a third one dimension array.
class arrayOneD {

  public static void main(String args[]) {

    int ar1[] 3567};
    int ar2[] 4217};
    int ar3[] new int[5];

    System.out.println("Array1 is ");

    for (int x = 0; x < ar1.length; x++) {
      ar3[x= ar1[x+ ar2[x];
      System.out.print(ar1[x"  ");
    }

    System.out.println("Array2 is ");

    for (int x = 0; x < ar1.length; x++) {
      System.out.print(ar2[x"  ");
    }

    System.out.println("sum of two array is ");

    for (int x = 0; x < ar1.length; x++) {
      System.out.print(ar3[x"  ");
    }
  }
}
Output
Array1 is
3  5  6  7  8

Array2 is
4  2  1  7  6

sum of two array is
7  7  7  14  12
Download


Example sorting of an given array of 5 element
public class arraySort {
  public static void main(String[] args) {
    int myarr[] 6778344 };
    int l = myarr.length;
    int temp;

    for (int x = 0; x < l; x++)
      for (int y = 0; y < l - 1; y++) {
        if (myarr[y>= myarr[y + 1]) {
          temp = myarr[y];
          myarr[y= myarr[y + 1];
          myarr[y + 1= temp;
        }
      }// no is the total no of array element
    for (int x = 0; x < myarr.length; x++)
      System.out.print("  " + myarr[x]);
  }
}
Output
3  6  8  44  77

Download



Two Dimensional Array
Array can be two dimensional.In this array can be represented
in both the dimension i.e. X cordinate and Y cordinate .



Syntax
data-type variable-name[no1][no2]

no1 is the total  no of row  array element
no2 is the total  no of column array element

For ex 2*2 is

2 3
3 4

2*3 is

2 4 5
1 2 3

3*3 is 1

1 2 3
4 5 6
7 8 9


Example
Take a 3*2 two  dimensional array.
Initialize the elements and display it


class twoD {
  public static void main(String args[]) {
    int arr[][] { { 1112}3455 }6789 } };
    for (int x = 0; x < 3; x++) {
      for (int y = 0; y < 2; y++) {
        System.out.print(arr[x][y"  ");
      }
      System.out.print("\n");
    }
  }
}
Output

11  12 
34  55 
67  89 


Download



import java.util.Scanner;

public class ArrayTest {

  public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter no of students");
    int n = sc.nextInt();

    String name[] new String[n];

    int data[][] new int[n][5];

    String a[] "roll""marks1""marks2" };

    for (int i = 0; i < n; i++) {
      System.out.println("Enter name");
      name[i= sc.next();

      for (int j = 0; j < a.length; j++) {

        System.out.println("Enter " + a[j]);
        data[i][j= sc.nextInt();
      }
      data[i][3= data[i][1+ data[i][2];
      data[i][4= data[i][32;
    }
    String a1[] "roll""marks1",
"marks2"
"total""percentage" };
    for (int i = 0; i < n; i++) {

      System.out.println("student" (i + 1)
 
"information : \n\nName "
          + name[i]);
      for (int j = 0; j < a1.length; j++) {

        System.out.println(a1[j" is " + data[i][j]);
      }

    }

  }
}
Output:-

java ArrayTest
Enter no of students
3
Enter name
javed
Enter roll
101
Enter marks1
75
Enter marks2
77
Enter name
shankar
Enter roll
102
Enter marks1
78
Enter marks2
87
Enter name
Sagar
Enter roll
103
Enter marks1
77
Enter marks2
68
student1information :

Name javed
roll is 101
marks1 is 75
marks2 is 77
total is 152
percentage is 76
student2information :

Name shankar
roll is 102
marks1 is 78
marks2 is 87
total is 165
percentage is 82
student3information :

Name Sagar
roll is 103
marks1 is 77
marks2 is 68
total is 145
percentage is 72
Ddownload code

Multidimensional Arrays

  • A Multi-dimensional array is an array of arrays
  • To declare a multidimensional array, specify each additional index
    using another set of square brackets
int [][] x;
x is a reference to an array of int arrays
x = new int[3][4];
Create 3 new int arrays, each having 4 elements
x[0] refers to the first int array, x[1] to the second and so on
x[0][0] is the first element of the first array
x.length will be 3

Example:-

class  arr2D
{
public static void main(String args[])
   {
int arr[][]={
           {11,12,},
           {34,55},
           {67,89}
          };
for(int x=0;x<3;x++)
  for(int y=0;y<2;y++)
    {
System.out.print(arr[x][y]+"  ");
     }
System.out.print("\n");
   }
}

Output:-
java arr2D
11  12  34  55  67  89
Download code

Example:-

public class CompoundInterest {
  public static void main(String[] args) {
    final double STARTRATE = 10;
    final int NRATES = 6;
    final int NYEARS = 10;
    // set interest rates to 10 . . . 15%
    double[] interestRate = new double[NRATES];
    for (int j = 0; j < interestRate.length; j++)
      interestRate[j(STARTRATE + j100.0;

    double[][] balances = new double[NYEARS][NRATES];

    // set initial balances to 10000
    for (int j = 0; j < balances[0].length; j++)
      balances[0][j10000;

    // compute interest for future years
    for (int i = 1; i < balances.length; i++) {
      for (int j = 0; j < balances[i].length; j++) {
        // get last year's balances from previous row
        double oldBalance = balances[i - 1][j];

        // compute interest
        double interest = oldBalance * interestRate[j];

        // compute this year's balances
        balances[i][j= oldBalance + interest;
      }
    }

    // print one row of interest rates
    for (int j = 0; j < interestRate.length; j++)
      System.out.printf("%9.0f%%"100 * interestRate[j]);

    System.out.println();

    // print balance table
    for (double[] row : balances) {
      // print table row
      for (double b : row)
        System.out.printf("%10.2f", b);

      System.out.println();
    }
  }
}
Output:-

java CompoundInterest
       10%       11%             12%        13%         14%           15%
  10000.00  10000.00  10000.00  10000.00  10000.00  10000.00
  11000.00  11100.00  11200.00  11300.00  11400.00  11500.00
  12100.00  12321.00  12544.00  12769.00  12996.00  13225.00
  13310.00  13676.31  14049.28  14428.97  14815.44  15208.75
  14641.00  15180.70  15735.19  16304.74  16889.60  17490.06
  16105.10  16850.58  17623.42  18424.35  19254.15  20113.57
  17715.61  18704.15  19738.23  20819.52  21949.73  23130.61
  19487.17  20761.60  22106.81  23526.05  25022.69  26600.20
  21435.89  23045.38  24759.63  26584.44  28525.86  30590.23
  23579.48  25580.37  27730.79  30040.42  32519.49  35178.76
Download code