Pin It

How to declare, create and access a one-dimensional Array in Java

Introduction :-
Today I will be explaining you some of the basic concepts related to an array. I will be teaching you how to declare an array ?. , how to create an array ?. and how to assign values to array elements?.Generally an array is group of variables holding values which have same data types. It means an array can store many values in them which are of same data types. Generally arrays are objects in Java, so they come under category of reference types. An array variable provides reference to an array object in a memory which can either hold primitives data types or reference data , based upon the declaration of an array. To retrieve elements of an array we use concepts of index.We provide the name of array variable and position number of array element which we want to retrieve in a square bracket. Let us go further and see how this whole concept works.


How to declare a One-Dimensional arrays ?.
A one-dimensional array is a linear list of elements of the same type. Let us see how to declare an One-Dimensional array:

Syntax : 
type array-name [ ]; 
Or 
type [ ] array-name;

Here, type declares the base type of the array and array-name is the name of the array.
Let us look into few of examples :

int [ ] intArray ; 
byte [ ] byteArray ;
Object [ ] objectArray;

In first example, we have declared an array which can hold int values. The array name is intArray. However, the array does not exist. The object intArray is set to null which represents an empty array.

How to create a One-Dimensional array ?.
In order to create a One-Dimensional array, we use new operator. The new operator creates an object of array in heap memory. Let us look into the syntax :

Syntax :
array-name = new type [ size ] ; 

Here array-name is the name of the array, type is the datatype of elements in the array and size is the number of elements.

Let us look into few of the examples :

intArray = new int [ 10 ] ;
byteArray = new byte [ 10 ];
objectArray = new Object [ 10 ] ;

In first example, we have created an integer array of 10 elements and assigns it to the variable intArray. After writing this syntax an array object is created in heap which can hold 10 integer values.


How to assign values to array through example ?.
Values can be assigned to an array by couple of ways. Let us look into syntax of some of those ways below :

Syntax :
array-name [ index ] = value ;

The value can be assign to each array element through their respective index number. As arrays are linear so they have sequential indexing. The indexing of an array starts with number 0.

Let us look into an example below :

for( int i = 0; i < 10 ; i++ )
intArray[ i ] = i + 1 ;

After for loop gets executed, values from 1 to 10 are assigned to array elements intArray[ 0 ] to intArray[ 9 ]. There is also an another way of assigning values to an array at the time of declaration. This assigning of value to an array is called as initialization. If we initialize an array, the size of an array need not be given. Let us look into syntax of initialization of an array.

syntax : 
type array-name [ ] = { value1, value2, value3 .... .... , valueN }
Let us look into the example:

int [ ] intArray = { 1,2,3,4,5,6,7,8,9,10} ;


Accessing Array Elements 

Generally, Arrays in Java are implemented as objects. Arrays in Java have a attribute called as length which stores the size of an array. Each and every array created has this attribute. It always store size of an array. This attribute or field can be accessed by using syntax :

array-name.length

Let us look into an example :

int [ ] intArray = { 4,5,6,7,8,9,1,2,3} ; 
for (int i = 0; i < intArray.length; i++ )
System.out.println( "Value of element" + i + " = " + intArray[ i ] );
Above code will print the values of an array element using length field. If we make an attempt to access array elements beyond the legal index, it throws an exception called ArrayIndexOutOfBoundsException.

Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot