Arrays in Java

Primitive data type in Java can only store a single value of a particular type. Arrays are used to store multiple values of a similar type into a single variable. Let’s straight away see an example of how to create an array in Java to have a better understanding.

int[] myIntArray = new int[10];
char[] myCharArray = new char[10];
double[] myDoubleArray = new double[10];
String[] myStringArray = new String[10];

The above example shows how you can define Array variable in Java. When you include a square bracket [] just after the data type. The Java compiler understands it as an Array. We can create of all the primitive types in Java and also for String class.
new int[10]; the number in the square bracket in this expression denotes the size of an array. When you say you want to create an array of 10 that means that you can store 10 int values in the same variable.

Now Since we have understood how to define an Array we can further look into how we can store values in an Array.

Storing value in Arrays

Livewire Component Library
//First Way
int[] myIntArray = new int[5];
int[0] = 10;
int[1] = 20;
int[2] = 30;
int[3] = 40;
int[4] = 50;

//Second Way
int[] myIntArray = {10,20,30,40,50};

//Third Way
int[] myIntArray = new int[5];
for(int i=0; i < myIntArray.length; i++){
     myIntArray[i] = i * 10;
}

In the above code there are three ways to store same five values in Java Array. In the first method we create an Array of size 5 and store values at particular Array index one by one. Notice that to store the first value we are using the index 0 ,int[0] = 10 the internal index of Arrays in Java starts from 0. So even if you are storing your first element it will go at index 0.

Host Laravel Application on DigitalOcean

Use coupon 5balloons on this Cloudways Affiliate URL to get special discount.

If you know in advance the values which you want to store in your Array then second method is probably the best way to initialise and store values in Array. By using this way we are defining and storing values in Array in a single statement.

Third method is almost similar to the first one, in this we are using a loop to automate the value insertion at array indexes. Notice that we have use myIntArray.length as our termination value. length is a pre defined property defined in Java for arrays, it returns the size of our Array, which in this case is 5.

Retrieving value from Arrays
Retrieving the values from Arrays is mostly similar to storing.

int[] myIntArray = {10,20,30,40,50};
for(int i=0; i < myIntArray.length; i++){
     System.out.println(myIntArray[i]);
}

Notice that we are starting our loop initialisation from 0 and ending it one value before the length of the Array. So if a Array is of size 10. It will contain values from index 0 to 9. If try to retrieve or store value at a position which is outside the array size then we will receive an Exception. See example below.

int[] myIntArray = {10,20,30,40,50};
for(int i=0; i <= myIntArray.length; i++){  //Will produce exception
     System.out.println(myIntArray[i]);
}

The above code will produce exception Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5

Array Example Code

Let’s now see a complete example code of Array’s in Java.
The below program accepts 5 numbers from User to create an Array and Print It.

public class ArrayExample {
	
	public static Scanner scanner= new Scanner(System.in);

	public static void main(String args[]){
		int[] myIntArray = getIntegers(5);
		printArray(myIntArray);
	}
	
	public static int[] getIntegers(int number){
		System.out.println("Please Enter "+number+" integer values");
		int intArray[] = new int[number];
		for(int i=0; i<number; i++){
			intArray[i] = scanner.nextInt();
		}
		return intArray;
	}
	
	public static void printArray(int[] intArray){
		for(int i=0; i<intArray.length; i++){
			System.out.println(intArray[i]);
		}
	}
}

Exercise Questions
1. Extend the above code to accept 5 int values from user, Print the values and create another method to calculate the average of five values in the array.
2. Create a Java program to accept int values from user and create array from it, also create a method to sort the array. Create a separate method that accepts int Array as parameter and prints it. [Solution]

If you are looking to solve more Exercise Programming Questions related to Java Arrays. Then you can refer this Java Array : Practice Programming Questions and Solutions.

Site Footer