Array in java

In following example we will discuss about Array in Java. Array is a collection of data of same datatype.We can use it to store Integer, Boolean, String object. We can store only primitive data in array.

Array in java

In following example we will discuss about Array in Java. Array is a collection of data of same datatype.We can use it to store Integer, Boolean, String object. We can store only primitive data in array. We have contained multiples value of the same type and we can store the multiple value in memory at fixed size. We can use multiple type array. It can be used in Java, C++, PHP and any other programming languages.

We can use array in program as:

  • Declaring  Array
  • Constructing  Array
  • Initializing  Array

There are many syntax Declaration of an array:-

  • int [] anArray;  :-We have declared array of Integers and we have stored anArray of integer value.
  • int anArray= new int[4]; :- We have Stored only for 10 integer values
  • int AnArray[]={10,20,30,50,40,70,80}; :-We have Initialized an integer array of length 7 when First element is 10, Second element is 20 and as so on

Advantages of array:

  • We can use one name for similar objects and save with the same name but different indexes.
  • The array will always hold similar kind information.
  • When a programmer is working with same type of data, Array proves very useful.

Disadvantages of Array:

  • It becomes difficult to use many index Arrays.
  • It is not possible to change the size of an Array during runtime.
public class ArrayExample {
	public static void main(String[] args) {
		 int num[] = new int[7];
	        for (int i=0;i<7;i++){
	           num[i]=i+1;
	       }
	       for (int i=0;i<7;i++){
	           System.out.println("array["+i+"] = "+num[i]);
	       }
	      System.out.println("Length of Array  =  "+num.length);
	     }
		}

Download Source Code

Advertisements

 

We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.

Share on Google+Share on Google+

Array in java

Posted on: May 8, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Array in java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads