How to Create Initialising Arrays in Java?

 Initialising Arrays in JAVA

Array is the collection of similar elements which can be values or variables.

Initialising Arrays in Java 


There are several ways to initialize arrays in Java

Define and Initialize at same time:


int myArray[] = new int[] {10,21,330,5,32,50,61,83,60,23 };


Define and then initialize later:

int data[];
data=new int[] {10,20,30,40,50,60,71,80,90,91 };


Method 3 :


int[] data = {10,20,30,40,50,60,71,80,90,91};


One more method to initialize array in Java:


public class MyArray {
    int num[] = new int[10]; 
    
/** New instance of Array */
    public MyArray() {
        for(int i = 0; i < num.length; i++) {
            data[i] = i*10; // Any value
        }
    }
}


Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

0 comments:

Post a Comment