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

Java Arrays: Types, Syntax, and Examples

Java arrays are objects that store a fixed number of elements of the same data type in contiguous memory locations, allowing for efficient code optimization and random access. There are two types of arrays in Java: single-dimensional and multi-dimensional, with specific syntax for declaration, instantiation, and initialization. While arrays have advantages such as optimized code and easy data retrieval, they also have limitations, including a fixed size that does not allow for dynamic resizing.

Uploaded by

Rashmi Pal
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)
48 views4 pages

Java Arrays: Types, Syntax, and Examples

Java arrays are objects that store a fixed number of elements of the same data type in contiguous memory locations, allowing for efficient code optimization and random access. There are two types of arrays in Java: single-dimensional and multi-dimensional, with specific syntax for declaration, instantiation, and initialization. While arrays have advantages such as optimized code and easy data retrieval, they also have limitations, including a fixed size that does not allow for dynamic resizing.

Uploaded by

Rashmi Pal
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

Java - Arrays

array is a collection of similar type of elements that have contiguous memory location.

Java array is an object the contains elements of similar data type. It is a data structure where
we store similar elements. We can store only fixed set of elements in a java array.

Array in java is index based, first element of the array is stored at 0 index.

Advantage of Java Array

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

Disadvantage of Java Array

 Size Limit: We can store only 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.

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

1. dataType[] arr; (or)


2. dataType []arr; (or)
3. dataType arr[];
Instantiation of an Array in java

1. arrayRefVar=new datatype[size];

Example of single dimensional java array

Let's see the simple example of java array, where we are going to declare, instantiate,
initialize and traverse an array.

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

Output: 10
20
70
40
50

Declaration, Instantiation and Initialization of Java Array

We can declare, instantiate and initialize the java array together by:

1. int a[]={33,3,4,5};//declaration, instantiation and initialization

Let's see the simple example to print this array.

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

Output:33
3
4
5

Passing Array to 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 minimum number of an array using method.

1. class Testarray2{
2. static void min(int arr[]){
3. int min=arr[0];
4. for(int i=1;i<[Link];i++)
5. if(min>arr[i])
6. min=arr[i];
7.
8. [Link](min);
9. }
10.
11. public static void main(String args[]){
12.
13. int a[]={33,3,4,5};
14. min(a);//passing array to method
15.
16. }}

Output:3

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

1. dataType[][] arrayRefVar; (or)


2. dataType [][]arrayRefVar; (or)
3. dataType arrayRefVar[][]; (or)
4. dataType []arrayRefVar[];

Example to instantiate Multidimensional Array in java

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


Example to initialize Multidimensional Array in java

1. arr[0][0]=1;
2. arr[0][1]=2;
3. arr[0][2]=3;
4. arr[1][0]=4;
5. arr[1][1]=5;
6. arr[1][2]=6;
7. arr[2][0]=7;
8. arr[2][1]=8;
9. arr[2][2]=9;

Example of Multidimensional java array

Let's see the simple example to declare, instantiate, initialize and print the 2Dimensional
array.

1. class Testarray3{
2. public static void main(String args[]){
3.
4. //declaring and initializing 2D array

int arr[][]={{1,2},{2,4},{4,4}};

//printing 2D array

for(int i=0;i<3;i++){

for(int j=0;j<3;j++){

[Link](arr[i][j]+" ");

[Link]();

5. }
6.
7. }}

Output:1 2 3

You might also like