Home Java Beginners Arrayexamples Array declaration in Java
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Array declaration in Java

Advertisement
Here we have discussed how to declare an array with examples. Array is used to store values.

Array in Java can be used to store similar kind of data or element in a sequential manner. These data or elements can be logically grouped together. Array is easy to store, easy to iterate. Array's length is defined when it is being created, hence it holds a fixed number of value. To retrieve an element from an array, we must know it's index value.

We can give any name to an Array, however it should follow the predefined conventions:

int[] array_name;

We must assign memory to an array while declaring:

int[] array_name = new
int[10];

The index of the first element is always 0.

Array can be classified into two types:

  • Single/One dimensional
  • Multidimensional

Declaring an array in Java:

One dimensional array can be declared as:

int OneDArray[];
or
int[] OneDArray;

OneDArray[] can be instantiated as:

OneDArray =new int[10];

Here array can store up to 10 values of type int.

Initializing and Instantiating an Array:

int OneDArray[] = {98, 95, 91, 93, 97};

Array iteration

public class IterateOneDArray {
public static void main(String args[]) {

int OneDArray[] = {98, 95, 91, 93, 97};

//array iteration using foreach loop
for (int element: OneDArray){
System.out.println(element);
}
}
}

Array Sorting

sort() method is used to sort an array:

import java.util.Arrays;

public class OneDArraySort {
public static void main(String args[]) {
int OneDArray[] = { 98, 95, 91, 93, 97 };
System.out.println("Before sorting: " + Arrays.toString(OneDArray));
Arrays.sort(OneDArray);
System.out.println("After sorting: " + Arrays.toString(OneDArray));
}
}

Output:

Before sorting: [98, 95, 91, 93, 97]
After sorting: [91, 93, 95, 97, 98]

Copy Array:

copyof() method is used to copy array to another array:

import java.util.Arrays;

public class ArrayCopy {
public static void main(String args[]) {
String arr[] = { "a", "b", "c" };
String copyArr[] = Arrays.copyOf(arr, 3);
System.out.printf("Value of arr \t\t%s\n", Arrays.toString(arr));
System.out.printf("Value of copyArr\t\t%s", Arrays.toString(copyArr));
}
}

Output:

Value of arr [a, b, c]
Value of copyArr [a, b, c]

Multidimensional Array

Multidimensional arrays are arrays of arrays.

Declaration of two dimensional array:

int twoDArray[][];
or
int[][] twoDArray;

Instantiating twoDArray[]:

int twoDArray[][] = new int[4][5];

Here it means this two array have 4 rows and 5 columns.

Initializing and Instantiating the array:

int twoDArray[][] = { { 2, 1, 3 }, { 1, 3, 2 }, { 3, 2, 1 } };

Iterating the array:

public class IterateTwoDArray {
public static void main(String args[]) {

int twoDArray[][] = { { 2, 1, 3 }, { 1, 3, 2 }, { 3, 2, 1 } };

for (int row = 0; row < twoDArray.length; row++) {
for (int col = 0; col < twoDArray[row].length; col++) {
int element= twoDArray[row][col];
System.out.print(element+" ");
}
System.out.println();
}
}
}

Output:

2 1 3
1 2 2
3 2 1

Resource

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 8, 2013

Related Tutorials

Ask Questions?    Discuss: Array declaration in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com