Array in Java - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript Array in Java - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Friday, March 15, 2019

Array in Java

Array in Java


Multi-Dimensional Arrays


üA two-dimensional array can be declared as

  int twoD [] [] = new int[4][5];

Example

int tmp[][] = new int[3][3];

  int i,j;
 
  Scanner test=new Scanner(System.in);
 
  for(i=0;i<3;i++)

  for(j=0;j<3;j++)

  {

  System.out.print("Enter Input:");

  if(test.hasNextInt())

  tmp[i][j]=test.nextInt(); 

  }
 
  for(i=0;i<3;i++)

  {

  for(j=0;j<3;j++)

  System.out.print(tmp[i][j] + " ");
 
  System.out.println();

  }

Alternative Array Declaration Syntax

üType [] var_name;
üExample: int [] a = new int[3];
üint [] num1, num2, num3;
  same as
   int num1[], num2[], num3[]; 

Array as an Object

üAn array is implemented as an object.

üIt has a special attribute: length instance variable.

üExample:

  int a1[] = new int[10];

  int a2[] = {3, 5, 7, 9, 11, 13, 15, 17};

    int a3[] = {4, 3, 2, 1};

    System.out.println(length of a1 is: +a1.length);
    System.out.println(length of a2 is: +a2.length);
    System.out.println(length of a3 is: +a3.length);

No comments:

Post a Comment

Post Top Ad