0% found this document useful (0 votes)
1 views5 pages

Arrays in Java

The document provides an overview of arrays in Java, describing them as objects that store similar data types in contiguous memory locations. It outlines the advantages and disadvantages of using arrays, types of arrays (single-dimensional and multidimensional), and includes syntax for declaring, instantiating, and initializing arrays. Additionally, it discusses the ArrayIndexOutOfBoundsException and provides examples of using arrays, including a for-each loop and taking input from users.

Uploaded by

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

Arrays in Java

The document provides an overview of arrays in Java, describing them as objects that store similar data types in contiguous memory locations. It outlines the advantages and disadvantages of using arrays, types of arrays (single-dimensional and multidimensional), and includes syntax for declaring, instantiating, and initializing arrays. Additionally, it discusses the ArrayIndexOutOfBoundsException and provides examples of using arrays, including a for-each loop and taking input from users.

Uploaded by

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

Arrays In Java

• Normally, an array is a collection of similar type of elements which have a


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 sizeof 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
• 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.

1
Types of Array in java
• There are two types of array.

• Single Dimensional Array

• Two Dimensional(2D) or Multidimensional Array

Single Dimensional Array in Java


Syntax to Declare an Array in Java

• dataType[] arr; (or)

• dataType []arr; (or)

• dataType arr[];

Instantiation of an Array in Java

• arrayRefVar=new datatype[size];

Example of Java Array

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

Declaration, Instantiation and Initialization of Java Array


• We can declare, instantiate and initialize the java array together by:
• int a[]={33,3,4,5};//declaration, instantiation and initialization
• Let's see the simple example to print this array.

2
class Testarray1{
public static void main(String args[]){
int a[]={33,3,4,5};//declaration, instantiation and initialization
//printing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}}

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:
for(data_type variable:array)
{
//body of the loop
}
Example :-
class Testarray1{
public static void main(String args[]){
int arr[]={33,3,4,5};
//printing array using for-each loop
for(int i:arr)
System.out.println(i);
}}

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.

• //Java Program to demonstrate the case of

• //ArrayIndexOutOfBoundsException in a Java Array.

public class TestArrayException{


public static void main(String args[]){
int arr[]={50,60,70,80};
for(int i=0;i<=arr.length;i++){
System.out.println(arr[i]);
}
}}

3
// Example to add values entered into array
import java .util.Scanner;
public class TakingInput
{
public static void main(String[] args) {
int sum = 0;
Scanner s=new Scanner(System.in);
System.out.println("enter number of elements");
int n=s.nextInt();
int arr[]=new int[n];
System.out.println("enter elements");

for(int i=0;i<n;i++){
//for reading array
arr[i]=s.nextInt();
sum = sum + arr[i];
}
System.out.println("Sum:"+sum);
/*for(int i: arr)
{
//for printing array
System.out.println(i);
} */

}
}

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

• dataType[][] arrayRefVar; (or)

• dataType [][]arrayRefVar; (or)

• dataType arrayRefVar[][]; (or)

• dataType []arrayRefVar[];

4
Example to instantiate Multidimensional Array in Java

int[][] arr=new int[3][3];//3 row and 3 column

Example to initialize Multidimensional Array in Java

• arr[0][0]=1;
• arr[0][1]=2;
• arr[0][2]=3;
• arr[1][0]=4;
• arr[1][1]=5;
• arr[1][2]=6;
• arr[2][0]=7;
• arr[2][1]=8;
• arr[2][2]=9;
• //Java Program to illustrate the use of multidimensional array

class Testarray3{
public static void main(String args[]){
//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}}

You might also like