Java Array (with Examples)

An array is a container object that holds a fixed number of values of a single type in a contiguous memory location. It is a data structure that is used to store a finite number of elements, and all elements must be of the same data type.

Arrays are index-based data structures that allow random access to elements, they store. Indices start with '0'.

1. Array Representation in Memory

In this example, we have created an array of 5 elements. Indexes will range from '0' to '4'.

int[] a = new int[5];
 
a[0] = 1;
a[1] = 2;
a[2] = 4;
a[3] = 8;
a[4] = 16;

A pictorial representation of the above example can be as below.

Array in memory

2. Features of an Array

  • Arrays are also a subtype of Object in Java.
  • Arrays are objects so we can find the length of the array using the attribute 'length'.
  • Java arrays are types. we can declare the variables of array type.
  • Arrays are ordered and each array has an index beginning from '0' for the first element.
  • Arrays can store primitives as well as objects. But all must be of a single type in one array instance.
  • Just like other variables, arrays can also be static, final or used as method arguments.
  • The size of an array must be specified by an int value.
  • Java arrays are Cloneable and Serializable.

3. Types of Arrays

An array can be either of the following two types:

3.1. Single-Dimensional Array

An array that stores only primitives or objects is called a single-dimensional array. The general form of a one-dimensional array declaration is:

type var-name[];
OR
type[] var-name;
 
//Examples
 
int[] numbers;
 
String names[];

3.2. Multi-Dimensional Array

A multi-dimensional array stores other arrays.

It is array of arrays. In a multi-dimensional array, each array element holds the reference of other arrays. A multidimensional array is created by appending one set of square brackets ([ ]) per dimension.

type var-name[][];
OR
type[][] var-name;
 
//Examples
 
int[][] cordinates;
 
String nameSets[][];

4. Basic Operations on Arrays

4.1. Initializing an Array

The syntax for creating an array with pre-defined values.

String status[] = { "Active", "Inactive", "Purged" };

//or

String status[] = new String[] { "Active", "Inactive", "Purged" };

4.2. Iterating over Items

Use the standard for-each loop to iterate over the items of an array.

String status[] = { "Active", "Inactive", "Purged" };

for(String s : status) 
{
        System.out.println(s);
}

4.3. Printing Arrays

The recommended way to print the content of a simple array is using Arrays.toString().

System.out.println( Arrays.toString( status ) );

Use Arrays.deepToString() to print multi-dimentional arrays.

System.out.println(Arrays.deepToString( arrayOfArray )); 

4.4. Finding Max and Min

The Stream interface provides two methods max() and min() that return the largest and the smallest item from the underlying stream. We can use these methods on the stream obtained from an Array.

int max = Arrays.stream(arrayOfInts)
  .max()
  .getAsInt(); 

int min = Arrays.stream(arrayOfInts)
  .min()
  .getAsInt(); 

5. Conclusion

In this Java Array tutorial, we learned the basic characteristics of arrays in Java. We also learned to perform very basic operations on array items. You can refer to these array tutorials to enhance your knowledge.

Happy Learning !!

Sourcecode on Github

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode