Friday, September 25, 2015

Arrays


An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. In other words,you can say that an array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
Ok! now after a short definition of an array, let me tell you that Arrays are basically classified into 3 types:

So,How to declare array in java?

One Dimensional Array

Syntax for default values:
int[] num = new int[5];
Syntax with values given:
int[] num = {1,2,3,4,5};

Multidimensional array

Declaration

int[][] num = new int[5][2];

For primitive types:

int[] myIntArray = new int[3];
int[] myIntArray = {1,2,3};
int[] myIntArray = new int[]{1,2,3};
For classes, for example String, it's the same:
String[] myStringArray = new String[3];
String[] myStringArray = {"a","b","c"};
String[] myStringArray = new String[]{"a","b","c"};
Apart from Syntaxs,keep the following array figures in your mind:
Basic array figure


This one denotes 1-D array

No comments:

Post a Comment