Previous
Next

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.

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.

Share This Page On :

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 ?

What are the Advantages of Java Array ?

What are the Disadvantages of Java 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.

    

More Related Tutorials

Previous
Next