Arrays in Java:
1. Array in java is similar to pronounced as Java Array. Normally, an array is a collection of similar
types of elements which has contiguous memory location.
2. 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.
3. In Java, an array is an object of a dynamically generated class. Java array inherits the Object
class, and implements the Serializable as well as Cloneable interfaces.
4. 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.
5. 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.
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 , consist of Single Dimensional Array & Multidimensional Array.
Single Dimensional Array :
1. A single-dimensional array in Java is a linear collection of elements of the same data
type.
2. It allows you to store multiple values of the same type in a single data structure.
3. Each element in the array is identified by an index, which represents its position within
the array.
Syntax to declare Single Dimensional Array in JAVA:
dataType[] arrayname; (or)
dataType []arrayname; (or)
dataType arraynamer[]; // We used this syntax in our program code.
Instantiation of an Array in Java:
datatype arrayname[]=new datatype[size];
Initialization of an Array in Java:
Method 1-:
arrayname[0]={value1};
arrayname[1]={value2};
arrayname[2]={value2};
|
|
arrayname[n]={valuen};
Method2:
Declaration, Instantiation and Initialization of Java Array:
int arrayname[]={value1, value2, value, ….};
Print or traversing single dimensional array
Method1:
for(int i=0;i<[Link];i++)
{
[Link](arrayname[index]);
}
Note: length is size property of Array. We can write here array size.
Method 2:
The syntax of the for-each loop is given below:
for(data_type variable:arrayname){
//body of the loop
}
Example-:
for(data_type p:arrayname){
[Link](p);
}
Note : p is variable of datatype & array name is variable of Array.
Program for Single Dimensional Array:
Public class MajhaSingleDArray{
public static void main(String args[]){
int a[]={77,33,44,55};//declaration, instantiation and initialization
//printing array
[Link](“My Single Dimensional Array:\t”);
for(int i=0;i<[Link];i++)//length is the property of array
[Link](a[i]+” ”);
}
Output: My Single Dimensional Array: 77 33 44 55
Multi Dimensional Array :
1. A multi-dimensional array in programming is an extension of the concept of a
single-dimensional array, but it allows you to organize elements in a grid-like structure
with multiple dimensions.
2. Each dimension is essentially a separate array, and you access elements using multiple
indices, one for each dimension.
3. Multi-dimensional arrays are commonly used to represent matrices, tables, grids etc
Syntax to declare Single Dimensional Array in JAVA:
dataType[][] arrayname; (or)
dataType [][]arrayname; (or)
dataType arrayname[][]; (or) // We used this syntax in our program code.
dataType []arrayname[];
Instantiation of an Array in Java:
datatype arrayname[][]=new datatype[size][size];
Initialization of an Array in Java:
Method 1-:
arrayname[0][0]={value1};
arrayname[0][1]={value2};
arrayname[1][0]={value2};
|
|
arrayname[n]={valuen};
Method2:
Declaration, Instantiation and Initialization of Java Array:
int a[][]={{value1, value2, value},{value1, value2, value},{value1, value2, value}, ….};
Print or traversing Multi Dimensional array
for(int i=0;i<[Link];i++)
{ for(int i=0;i<[Link];i++)
{
[Link](arrayname[index i ][index j]); // for matrix , we use
[Link](a[][]);
}
[Link]();
}
Note: length is size property of Array. We can write here array size.
Program for Single Dimensional Array:
Public class MajhaMutliDArray{
public static void main(String args[]){
int a[][]={{77,33},{44,55}};//declaration, instantiation and initialization
//printing array
[Link](“My Muli Dimensional Array: \n”);
for(int i=0;i<2;i++)//row
for(int j=0;i<2;j++) //column
[Link](a[i][j]+” ”);
[Link]();
}
Output: My Multi Dimensional Array:
77 33
44 55