Java – Arrays3 min read

An array is a group or the collection of data having the same data-type. Arrays are objects in Java and with the fixed size(disadvantage). Elements are arranged inside the array with index numbers starting from zero as the first elements.
Random access of any element present in the array and code optimization feature makes it more useable.

Types of array in java:

  • Single Dimensional Array. 
  • Multidimensional Array 

One Dimensional Array:

One-dimensional array in Java is an array with a bunch of values that are declared with a single index.

Array declaration:

Example:

An array declaration has two components:
The type that is the data-type of an element and the name that is given by user choice with the brackets[].

Array initialization:

To initialize an array new is used and it appears as follows:

Example:

We can also set by combining declaration and initialization in the following ways:

Accessing an array element:

Array elements can be accessed by using its index number, where each element is assigned by one index number.
Example:

Example: Click here


Multi-Dimensional Array (Jagged Array)

Declaring an array of arrays is known as Multidimensional Array or Jagged Arrays. In simple words, it is an array of arrays. It is created by appending a square brackets “[]” for each dimension. Here data are stored in a tabular form that is in row-major order.

Syntax:

Declaration:

Instantiate Multidimensional Array in Java:

Array initialization:

We can also declare and initialize the 2D array together:

Example: Click here


Passing Array to Methods:

Similar to a variable, an array can also be passed to methods. To pass an array users need to call the array with its name and without the square roots within the method call. Let us see with an example below:

Example: Java program to sum the array values.

Output:


Returning Arrays from Methods:

As array can be passed to methods, it can also be returned from the array.
The following shows an example of it.

Output:

The foreach Loops:

It is also possible to print the array using its for-each loop. for-each loop prints the array elements one by one, by holding an array element in a variable, then executing the body of the loop.

Example of foreach loop in Java:

Output:


MORE

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More