• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer
  • Home
  • Java Tutorial
  • Java Posts
  • Node.js
  • Spring Core
  • Algorithms
  • Docker
  • Blogging
  • Misc
Tech Stack Journal

Tech Stack Journal

Arrays

Contents

  • 1 Declaration and Initialization of an Array
  • 2 Accessing an element of an Array
  • 3 Length of an Array
  • 4 Iterate over an Array
  • 5 ArrayIndexOutOfBoundsException

In Java, an array is an object that contains fixed set of items that are of same type.

Declaration and Initialization of an Array

Array variables of type primitive are different than ordinary primitive variables. An ordinary primitive variable will hold a value in its allocated memory location. Whereas, an Array variable, which is a reference type variable, hold the memory address of an Array object.

int[] arr1 = new int[5];

Here, an Array object reference variable is declared and initialized with an Array object of size 5. All of the elements in that object will be of type int and are initialized to 0.

int[] arr2 = {1, 2, 3, 4, 5};

If the elements of an Array are predetermined, you may declare it as above. Here, an Array object reference variable is declared and initialized with 5 element Array object.

int[] arr3 = null;

Here, an Array object reference variable is declared with null, that is, this reference variable is not yet pointing to any Array object.

Accessing an element of an Array

int[] arr = {1, 2, 3, 4, 5};
System.out.println(arr[2]);
arr[2] = 30;

To access an individual element from an Array, we need to use index of that element within square brackets. An index of an Array is like a serial number, but it will start with 0. So, in the above code snippet, arr[2] will return 3.

Length of an Array

Array objects are provided with an attribute length which returns the size of an Array.

int[] arr1 = {1, 2, 3, 4, 5};
System.out.println(arr.length);
5

Iterate over an Array

Yet times you may want to iterate over an array to process individual element containing in that array. In order to do that, we can use a loop as below:

package com.techstackjournal;

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

		int[] arr = { 4, 66, 31, 37, 40 };

		for (int i = 0; i < arr.length; i++) {
			if (arr[i] % 2 == 0) {
				System.out.println(arr[i] + " is even number");
			} else {
				System.out.println(arr[i] + " is odd number");
			}
		}

	}
}
4 is even number
66 is even number
31 is odd number
37 is odd number
40 is even number
iarr.lengthi<arr.lengtharr[i]arr[i] % 2 == 0Printi++
05true4true4 is even number1
15true66true66 is even number2
25true31false31 is odd number3
35true37false37 is odd number4
45true40true40 is even number5
55falseNENENENE
NE – Not Executed

ArrayIndexOutOfBoundsException

Trying to access a non existent Array element will throw an Exception called as ArrayIndexOutOfBoundsException. In Java, JVM signals an error in the form of an exception, when it finds violation of semantic constraints of the programming language.

So, whenever you come across this kind of exception check the flow of your program and identify where exactly it is referring non-existent array element.

package com.techstackjournal;

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

		int[] arr = { 10, 20, 30, 40, 50 };

		for (int i = 0; i <= arr.length; i++) {
			System.out.println(arr[i]);
		}

	}
}
10
20
30
40
50
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
	at com.techstackjournal.Arrays.main(Arrays.java:9)

Above program throws ArrayIndexOutOfBoundsException as it tries to access element at index 6 which is non-existent.

Primary Sidebar

More to See

Arrays.asList in Java Examples

February 21, 2021 By Admin

[Solved] Why List.add throws UnsupportedOperationException in Java?

February 20, 2021 By Admin

Secondary Sidebar

  • Java Tutorial
    • Introduction
    • Install Java
    • First Java Program
    • Statements and Comments
    • Packaging the Classes
    • Variables
    • Primitive Data Types
    • Operators
    • if-else statement
    • Loops in Java
    • Arrays
    • break and continue
    • Switch Statement
    • Classes and Objects
    • Methods
    • Encapsulation
    • Constructor in Java: All You Need To Know
    • Inheritance
    • Access Modifiers
    • Static Variables and Methods
    • Final keyword in Java
    • Abstract Class
    • What is an Interface in Java?
    • Method Overloading
    • Java – Method Overriding
    • Polymorphism
    • Java – super Keyword
    • Nested Classes
    • Exception Handling
    • String Class

Categories

  • Algorithms
  • Blogging
  • Docker
  • Java
  • Misc
  • Node.js
  • Spring Core
  • Windows

Archives

  • February 2021 (6)
  • January 2021 (1)
  • December 2020 (1)
  • September 2020 (2)
  • August 2020 (5)
  • July 2020 (4)
  • June 2020 (1)
  • May 2020 (4)
  • April 2020 (22)
  • November 2019 (3)
  • September 2019 (2)
  • August 2019 (6)

Footer

Navigation

  • Home
  • Java Tutorial
  • Java Posts
  • Node.js
  • Spring Core
  • Algorithms
  • Docker
  • Blogging
  • Misc

Recent

  • How to Make File Explorer Open to This PC instead of Quick Access in Windows 10
  • Arrays.asList in Java Examples
  • [Solved] Why List.add throws UnsupportedOperationException in Java?
  • How to Convert an Array to List in Java?
  • How Many Spaces in a Tab?

Search

Copyright © 2021 · Tech Stack Journal · Log in