Powered by Blogger.

What is Array in Java - An Array Statements and Program Solution

What is Array in Java:

An array is a group of variables of the same data type referable by a common name. The data type can be either a primitive data type or an class or reference type.

Array Example:

 public class ArrayTest{
 public static void main(String[]args){
 int[]myArray; //declaring an array
 myArray = new int[5]; // creating array
 // int[]myArray = new int[5];
 for(int i=0;i<5;i++)
 myArray[i]=100+i; // initializing array
 for(int i=0;i<=4;i++)
 System.out.println(myArray[i]);
 }
 }

Object Using Array Example:

 class Bird{
 public void fly(){
 System.out.println(”Bird can fly”);
  }
 }

Array Simple Example:

 public class ClassExample{
 public static void main(String[]args){
 Bird[]myArray = new Bird[3];
 for(int i=0;i<3;i++)
 myArray[i] = new Bird();
 for(int i=0;i<3;i++)
 myArray[i].fly();
  }
 }

0 comments:

Post a Comment