Single and Multidimensional arrays in java
Java Arrays
Single and multidimensional arrays in java
Normally, an array is a collection of similar type of elements which has contiguous memory location.
Java array is an object which contains elements of a similar data type. Additionally, The elements of an array are stored in a contiguous memory location. 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 0th index, 2nd element is stored on 1st index and so on.
Unlike C/C++, we can get the length of the array using the length member. In C/C++, we need to use the size of operator.
In Java, array is an object of a dynamically generated class. Java array inherits the Object class, and implements the Serializable as well as Cloneable interfaces. We can store primitive values or objects in an array in Java. Like C/C++, we can also create single dimensional or multidimensional arrays in Java.
Moreover, Java provides the feature of anonymous arrays which is not available in C/C++.

Advantages
- 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.
Disadvantages
- Size Limit: We can store only the fixed size of elements in the array. It doesn't grow its size at runtime. To solve this problem, collection framework is used in Java which grows automatically.
Types of Array in java
There are two types of array.
- Single Dimensional Array
- Multidimensional Array
Single Dimensional Array in Java
Syntax to Declare an Array in Java
Instantiation of an Array in Java
Example of Java Array
Let's see the simple example of java array, where we are going to declare, instantiate, initialize and traverse an array.
Declaration, Instantiation and Initialization of Java Array
We can declare, instantiate and initialize the java array together by:
Let's see the simple example to print this array.
For-each Loop for Java Array
We can also print the Java array using for-each loop. The Java for-each loop prints the array elements one by one. It holds an array element in a variable, then executes the body of the loop.
The syntax of the for-each loop is given below:
Let us see the example of print the elements of Java array using the for-each loop.
Output:
33 3 4 5
Passing Array to a Method in Java
We can pass the java array to method so that we can reuse the same logic on any array.
Let's see the simple example to get the minimum number of an array using a method.
Output:
3
Anonymous Array in Java
Java supports the feature of an anonymous array, so you don't need to declare the array while passing an array to the method.
Returning Array from the Method
We can also return an array from the method in Java.
Output:
10 30 50 90 60
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.
Output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at TestArrayException.main(TestArrayException.java:5) 50 60 70 80
Multidimensional Array in Java
In such case, data is stored in row and column based index (also known as matrix form).
Syntax to Declare Multidimensional Array in Java
Example to instantiate Multidimensional Array in Java
Example to initialize Multidimensional Array in Java
Example of Multidimensional Java Array
Let's see the simple example to declare, instantiate, initialize and print the 2Dimensional array.
Output:
1 2 3 2 4 5 4 4 5
Thank you for visiting our blog
- Contribution-
- College- Vishwakarma Institute Of Technology
- Department- Sy- Chemical Engineering
- Members-
- 1) Kalash Uprikar - Roll no- 43 - PRN - 12010892
- 2) Kaustubh Nilakhe - Roll no - 46 PRN - 12010166
- 3) Omkar Kedari - Roll no - 47 PRN - 12010602
- 4) Rutuja Khalse - Roll no - 49 PRN - 12010349
- 5) Kinisha Khapre - Roll no- 50 PRN - 12010288
Comments
Post a Comment