0% found this document useful (0 votes)
156 views

Array in Java

An array is a data structure that stores a fixed number of elements of the same type. Arrays allow random access to elements via indices and can improve code performance over other data structures. However, arrays are limited in size and don't dynamically grow at runtime like collections. There are two main types of arrays in Java: single dimensional arrays that store elements in a single list; and multidimensional arrays that store elements in a row and column matrix structure.

Uploaded by

ETL LABS
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
156 views

Array in Java

An array is a data structure that stores a fixed number of elements of the same type. Arrays allow random access to elements via indices and can improve code performance over other data structures. However, arrays are limited in size and don't dynamically grow at runtime like collections. There are two main types of arrays in Java: single dimensional arrays that store elements in a single list; and multidimensional arrays that store elements in a row and column matrix structure.

Uploaded by

ETL LABS
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Array

ETL LABS PVT LTD – JAVA PROGRAMMING 122


Array
Array is an object which contains
elements of a similar data type. It is a data
structure where we store similar elements. First Index Element (at Index 8)
We can store only a fixed set of elements Indices
0 1 2 3 4 5 6 7 8 9
in a Java array. 3

Array in java is index-based, the first Array Length is 10


element of the array is stored at the 0
index.

ETL LABS PVT LTD – JAVA PROGRAMMING 123


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.
4
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.

ETL LABS PVT LTD – JAVA PROGRAMMING 124


Types of Array

Single Multidimensional
Dimensional Array
Array

12
ETL LABS PVT LTD – JAVA PROGRAMMING 5
Single Dimensional Array
Syntax of Declaration of an Array
dataType[] arr; (or)
dataType []arr; (or) 3
dataType arr[];

Syntax of Instantiation of an Array


arrayRefVar=new datatype[size];

ETL LABS PVT LTD – JAVA PROGRAMMING 126


Multidimensional Array
data is stored in row and column based
index (also known as matrix form).

Syntax to Declare
dataType[ ][ ] arrayRefVar; (or)
4 dataType [ ][ ]arrayRefVar; (or)
dataType arrayRefVar[ ][ ]; (or)
dataType [ ]arrayRefVar[ ];

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

ETL LABS PVT LTD – JAVA PROGRAMMING 127

You might also like