0% found this document useful (0 votes)
4 views4 pages

Arrays in JAVA

Java arrays are objects that store elements of the same data type in contiguous memory locations, with fixed size and index-based access. They can be single-dimensional or multidimensional and offer advantages like code optimization and random access, but have the limitation of fixed size. Java arrays also support anonymous arrays and inherit from the Object class, implementing Serializable and Cloneable interfaces.

Uploaded by

k58462549
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Arrays in JAVA

Java arrays are objects that store elements of the same data type in contiguous memory locations, with fixed size and index-based access. They can be single-dimensional or multidimensional and offer advantages like code optimization and random access, but have the limitation of fixed size. Java arrays also support anonymous arrays and inherit from the Object class, implementing Serializable and Cloneable interfaces.

Uploaded by

k58462549
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Java Arrays

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 dimentional or
multidimentional arrays in Java.

Moreover, Java provides the feature of anonymous arrays which is not available in C/C++.

Advantages
o Code Optimization: It makes the code optimized, we can retrieve or sort the data
efficiently.
o Random access: We can get any data located at an index position.

Disadvantages
o 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

Syntax to Declare an single dimensional 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.

1. //Java Program to illustrate how to declare, instantiate, initialize


2. //and traverse the Java array.
3. class Testarray{
4. public static void main(String args[]){
5. int a[]=new int[5];//declaration and instantiation
6. a[0]=10;//initialization
7. a[1]=20;
8. a[2]=70;
9. a[3]=40;
10. a[4]=50;
11. //traversing array
12. for(int i=0;i<a.length;i++)//length is the property of array
13. System.out.println(a[i]);
14. }}

Test it Now
Output:

10
20
70
40
50

Using for each loop:

Multi-dimensional array:
In such case, data is stored in row and column based index (also known as matrix form).
Syntax 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.

1. //Java Program to illustrate the use of multidimensional array


2. class Testarray3{
3. public static void main(String args[]){
4. //declaring and initializing 2D array
5. int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
6. //printing 2D array
7. for(int i=0;i<3;i++){
8. for(int j=0;j<3;j++){
9. System.out.print(arr[i][j]+" ");
10. }
11. System.out.println();
12. }
13. }}
Test it Now
Output:

123
245
445

You might also like