.

Java Arrays - Arrays in Java

Last updated on

Arrays in Java

Java Arrays

Introduction to Arrays

The Java array stores a sequential collection of elements of the same data type. In other words an array is a collection of data i.e. data structure in Java. Arrays in Java programming language are also objects.

  • An array is a group of like-typed variables that are referred to by a common name. Arrays in Java work differently than they do in C/C++.
  • Java array is an object which contains elements of a similar data type. It is a data structure where we store similar elements. We can store only a fixed set of elements in a Java array.
  • Array in java is index-based, the first element of the array is stored at the 0 index.
  • Java Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

What is Java Array ?

An array is a collection of variables of the same type. Array stores a fixed size sequential collection of elements of the same type. An array is used to store a collection of data. Each item in an array is called an element, and each element is accessed by its numerical index. In a real-world programming situation, you would probably use one of the supported looping constructs (for, while, and do-while) to iterate through each element of the array, rather than write each line individually. You will learn about the various looping constructs (for, while, and do-while) in the java looping or iteration statements of Control Flow section.

What are Arrays in Java ?

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. Each item in an array is called an element, and each element is accessed by its numerical index.

What is the Definition of an Array ?

An Array is an object that holds fixed number of values of homogeneous or similar data type. In other words an array is a Data Structure where we can store similar elements.

How many types of arrays are there in Java ?

There are two types of arrays in Java.

  1. Single Dimensional Array
  2. Multidimensional Array

How Many Types of Single Dimensional Arrays Are There ?

1D Array or Single Dimension Array

How Many Types of Multidimensional Arrays Are There ?

  1. 2D Array
  2. 3D Array

What is 1D Array or Single Dimension Array ?

1D Array or Single Dimension Array is an array which has only one row or only one column in that array.

What is 2D Array ?

1D Array or Single Dimension Array is an array which has only one row or only one column in that array.

What is multidimensional ( Array of Arrays ) array in Java ?

Multidimensional array is an array which has multiple rows and multiple columns in it.

The Java multidimensional arrays are arranged as an array of arrays i.e. each element of a multi-dimensional array is another array. The representation of the elements is in rows and columns. Thus, you can get a total number of elements in a multidimensional array by multiplying row size with column size.

Java also supports arrays with more than one dimension and these are called Multidimensional arrays. The Multi-Dimensional arrays are Two-Dimensional array or 2D array, Three-Dimensional Arrays or 3D arrays and so on.

What is a 2d array in Java ?

A 2D array has a type such as int[][] , String[][], long[][], double[][], boolean[][], byte[][], short[][], float[][] with two pairs of square brackets. The elements of a 2D array are arranged in rows and columns, and the new operator for 2D arrays specifies both the number of rows and the number of columns.

About Java Arrays ?

  • In Java all arrays are dynamically allocated.
  • Since arrays are objects in Java, we can find their length using member length. This is different from C/C++ where we find length using sizeof.
  • A Java array variable can also be declared like other variables with [] after the data type.
  • In Java all the arrays are indexed and declared by int only.
  • The variables in the array are ordered and each have an index beginning from 0.
  • Java array index ends at 2147483646.
  • It is compulsory to declare the size of an array at the time of creation.
  • We can declare and create Java array with in a single line as int[] a = new int[3];
  • If we declare size of an array as 0 ( zero ) int[] a = new int[0]; then program will successfully compile and execute.
  • If we declare size of an array as negative i.e. int[] a = new int[-3]; then the program will compile successfully but when we run the program, it will throw NegativeArraySizeException.
  • The elements of array are stored in consecutive memory locations.
  • We can not initialize an array more than its size.

What are the Advantages of Java Array ?

  • Code Optimization: It makes the code optimized, we can retrieve or sort the data efficiently.
  • Random access: We can get any data located at an index position.
  • We can change the value of any indexed variable or value.
  • We can store 2147483647 elements in Java array.
  • Arrays are fast as compared to primitive data types.

What are the Disadvantages of Java Array ?

  • Size Limit: We can store only the fixed size of elements in the array.
  • Java array size does not grow after the allocation.
  • If you try to store long (big) elements in array, you will get performance problems.
  • Memory Wastage : There is a lot of chances of memory wastage. Suppose we created an array of length 100 but only 10 elements are inserted, the 90 blocks are empty and thus memory wasted.
  • Strongly Types : In Arrays we can store similar data types only. They are strongly typed.
  • Reduce Performance : The elements of arry are stored in consecutive memory location, thus to delete an element in an array we need to traverse through out the array. So this will reduce performance.
  • No Methods : Arrays does not have add or remove methods to add or delete elements in array.

Declaring Java Array Variable

Java Arrays are declared with [] (square brackets). If you put [] (square brackets) after any variable of any type only that variable is of type array remaining variables in that declaration are not array variables those are normal variables of that type. If you put [] (square brackets) after any data type all the variables in that declaration are array variables. All the elements in the array are accessed with index. The array element index is starting from 0 to n-1 number i.e. if the array has 5 elements then starting index is 0 and ending index is 4. In this tutorial you can learn how to declare arrays, how to assign values to arrays and how get values from arrays.

// declares Java array of integers
int[] javaArray;
int javaArray[];

What is the Length of an Array in Java ?

In Java all the arrays are indexed and declared by int only. That is the size of an array must be specified by an int value and not long or short. All the arrays index beginning from 0 to ends at 2147483646. You can store elements upto 2147483647. If you try to store long (big) elements in array, you will get performance problems. If you overcome performance problems you should go to java collections framework or simply use Vector.

Syntax :

< data type > < variable >[];
< data type >[] < variable >;

In this tutorial you can learn Java Arrays with the following Java Primitive Data Types.

When will Java Virtual Machine ( JVM ) throws ArrayIndexOutOfBoundsException ?

The Java Virtual Machine ( JVM ) throws an ArrayIndexOutOfBoundsException if length of the array in negative, equal to the array size or greater than the array size while traversing the array.