Java Arrays Example

An Array is a data structure which represents a group of elements of same data type. It can store a group of elements either it may be variable or objects. We can define the length of the array, when the array is created. We cannot store some integer values and some string values together in the array. To store ā€˜Nā€™ number of data, arrays are very useful to declare it in one or two statements.

also read:

By declaring array, it is possible to allocate memory space for values of a particular type. The elements are accessed by using index which starts at 0. Thus if we want to access the fourth index value, then it should be considered as fifth element in the array.

Arrays Advantages

  • Arrays contain multiple items of the same data type by using one variable name.
  • Array can jump to any element at any time when it is required to use.
  • Arrays are created on dynamic memory allotted at runtime by JVM.
  • Arrays can access the elements randomly using indexes.

Arrays Disadvantages

  • It is difficult to add or remove the elements, because elements are stored in consecutive memory locations.
  • To delete one element , we should traverse throughout the array.
  • We cannot increase or decrease the memory allocated to the array, since array is a static structure.
  • Since array is fixed size, there can be wastage or shortage of the memory if we allocate more memory than requirement.

Types of Arrays

There are two types of Arrays in Java.

  • One Dimensional Array
  • Two Dimensional Array

One Dimensional Array

A one dimensional array represents row or column of elements. It is a list of variables of the same type which can be accessed through common name. It represents a list of items by using only one variable. To create an array, we must create an array variable of the desired type.
The general form of a one-dimensional array declaration:

int score[ ] = new int[ ];

where

  • int specifies the base type of an array.
  • score[ ] indicates the type of array name.
  • int[ ] declares the size of an array.

One Dimensional Array Example

Below example demonstrate a one dimensional array:

package myarray;
public class Array_OneDimensionalExample {
	public static void main(String[] args) {
		int weight[ ]= new int[5];

		weight[0]=36;
		weight[1]=42;
		weight[2]=24;
		weight[3]=10;
		weight[4]=50;

	System.out.println("Weight of first person : " + weight[0]);
	System.out.println("Weight of second person : " + weight[1]);
	System.out.println("Weight of third person : " + weight[2]);
	System.out.println("Weight of fourth person : " + weight[3]);
	System.out.println("Weight of fifth person : " + weight[4]);
		}
}
  • In the above example we have declared array as
    int weight[]= new int[5]; where int is a type of data, weight[ ] is an array name, int[5] is the size of an array.
  • We have initialized the array by using values
    weight[0]=36; weight[1]=42; weight[2]=24; weight[3]=10; weight[4]=50;

When you run the above example, you would get the following output.

Java One dimensional Array Example

Two Dimensional Array

A two dimensional array is used to represents the model data with rows and columns of data. It is a combination of two or more, one dimensional array and defines the size of rows and columns respectively. To declare a two-dimensional array variable, specify each additional index using another set of square brackets.

The general form of a two-dimensional array declaration is:

int name[ ][ ]= new int[ a][b];

where

  • int specifies the base type of an array.
  • name[ ][ ] indicates the type of array name.
  • int[ a][b] declares the size of an array, where a represents the size of rows and b represents the size of columns.

Two Dimensional Array Example

Below example demonstrate a two dimensional array:

package myarray;
public class Array_TwoDimensionalExample {
	public static void main(String[] args) {
               int value[ ][ ]= new int[3][3];

		value[0][0]=1;
		value[0][1]=1;
		value[0][2]=1;

		value[1][0]=2;
		value[1][1]=2;
		value[1][2]=2;

		value[2][0]=3;
		value[2][1]=3;
		value[2][2]=3;

System.out.println(value[0][0] + " " + value[0][1] + " " + value[0][2]);
System.out.println(value[1][0] + " " + value[1][1] + " " + value[1][2]);
System.out.println(value[2][0] + " " + value[2][1] + " " + value[2][2]);
	}

}
  • In the above example, we have declared array as
    int value[ ][ ]= new int[3][3]; where int is a type of data, value[ ] is an array name, int[3][3] is the size of an array of row and column elements.
  • Array can initialized by using the values,
    value[0][ 0]=1; value [0][1]=1; value [0][2]=1;
    value [1][0]=2; value [1][1]=2; value [1][2]=2;
    value [2][0]=3; value [2][1]=3; value [2][2]=3;

When you run the above example, you would get the following output.

Java Two dimensional Array Example

Arrays Methods

MethodDescription
public static int binarySearch( Object[ ] a, Object key)Using binary search algorithm , it searches the specified array objects(int, char, float, double etc.) for the specified key.
public static boolean equals( Object[ ] a, Object[ ] a2)Returns true if specified array of objects(int, char, float, double etc.) contain same number of elements .
public static void fill( Object[ ] a, Object val)Assigns the specified object value to each of the element of the specified array objects(int, char, float, double etc.)
public static void sort( Object[ ] a)Returns the specified sorted array of objects(int, char, float, double etc.) in ascending order.
public static int hashCode( Object[ ] a)Returns the hashcode of the specified array objects(int, char, float, double etc.).
public static String toString( Object[ ] a)Returns the string presentation of the specified array object( int, char, float. double etc.).

Passing Array using Methods

An array can be passed through by one method to another method only when, the both methods should contain same data type. It is also called as pass by reference. When an array is passed as argument, then we call the memory address location as reference. By using methods, we can return the same array elements of the method into other method array element.

package myarray;

public class Array_MethodExample {

	public static void main(String[] args) {
		int[ ] marks= {66,77,88};
		test(marks);

		System.out.println("marks[0]= " + marks[0]);
		System.out.println("marks[1]= " + marks[1]);
		System.out.println("marks[2]= " + marks[2]);

	}
	public static void test(int[ ] num)
	{
		num[0]=10;
		num[1]=20;
		num[2]=30;
	}

}
  • In the above example we have declared array as
    int[ ]marks= {66,77,88}; where int is a type of data, marks[ ] is an array name, initialized the array by using values {66,77,88} .
  • test(marks); statement specifies test( ) method should be executed first by using initialized values.
  • We have used public static void test( ) method to pass the array elements by using values num[0]=10; num[1]=20; num[2]=30;

When you run the above example, you would get the following output.

Java Array Method Example

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*