Array in Java
Array in Java
Def : Array is Linear data structure which hold the homogenous data element in index format.
OR
Array in Java is a container object that holds a group or collection of elements of a similar data type.
Features of Arrays in Java
1. An array in Java is a sequence of variables with a similar data type. The variables are called elements or components of the array that contain values of the same type.
2. The elements of an array can be either primitive data types or reference types (including itself array type).
3. Elements in Java array are always numbered from 0 to (n – 1) where n is the individual elements in the array.
4. The position of an element in an array is called index of element .
5. The elements in the array are ordered or arranged in contiguous memory locations.
6. Arrays are created dynamically during runtime in Java, They are dynamic, created on the heap memory.
7. after creted array we cannot be changed. That is, the length of an array is fixed once the size of an array is created. Therefore, an array is not resizable.
Types of Arrays in Java
1. Single dimensional arrays (or 1D array called one-dimensional array)
2. Multidimensional arrays (or 2D, 3D arrays)
1. Single dimensional arrays (or 1D array called one-dimensional array)
-An array with one dimension is called one-dimensional array or single dimensional array in java.
-One dimensional array represents one row or one column of array elements that share a common name
Creating One dimensional Array
There are two way to create 1 dimesional array.
1.We can declare one-dimensional array and store values (or elements) directly at the time of its declaration, like this:
Syntax :
int marks[ ] = { 90, 97, 95, 99, 100 }; // declare marks[ ] and initialize with five values.
2. The second way of creating a one-dimensional array is by declaring array first and then allocating memory for it using the new keyword.
Syntax :
int marks[ ]; // declare marks arrays.
marks = new int[5]; // allocating memory for storing 5 integer elements into an array.
some examples for single dimensional array:
1. float salary[ ] = {99859.55f, 15000f, 14500.75f, 9050f};
2. float salary[ ] = new float[10];
3. char ch [ ] = {'s', 'c', 'i', 'e', 'n', 't', 'e', 'c', 'h', 'e', 'a', 's', 'y'};
4. char ch [ ] = new char [10] ;
5. String colors [ ] = {"Orange", "Red", "Sky blue", "Green"};
6. String names = new String[5];
Multidimensional Array in Java
- Multidimensional Array is internally conside as array of array .
- a two-dimensional array is a combination of two or more one-dimensional (1D) arrays.
- a three-dimensional array is a combination of two or more two-dimensional (2D) arrays.
Declare and Initialize Two Dimensional Array
1. The syntax to create a two dimensional array and directly store elements at the time of its declaration, as follows:
int marks[ ] [ ] = {{50, 60, 78, 87, 95},
{98, 87, 75, 76, 99},
{98, 99, 97, 95, 100}
};
0 1 2 3 4
0 50 60 78 87 95
1 98 87 75 76 99
2 98 99 97 95 100
2. The second way for creating a two dimensional array is that first declare an array and then allocate memory for it using the new operator.
The syntax to declare a two dimensional array 3-by-5 of type int is as follows:
int[ ][ ] marks; // declaring a marks array.
marks = new int[3][5]; // allocating memory for storing 15 elements.
int[][] sub = new int[2][];
sub[0] =new int[2];
sub[1] =new int[3];
Way to write
String str[ ][ ] = new String[2][3]; // Allowed but not preferred.
Or,
String[ ][ ] str = new String[2][3]; // Allowed and most preferred way.
take some examples for two dimensional arrays :
1. float[ ] [ ] x = {{1.1f, 1.2f, 1.3f },
{2.1f, 2.2f, 2.3f},
{3.1f, 3.2f, 3.3f}
};
2. double[ ] [ ] y = {{10.2, 15.5}, {1.5, 2.5}};
3. byte[ ][ ] b = new byte[2][5];
4. String[ ][ ] str = new String[ 3][3];
5. int[ ][ ] values = new int[3][4];
Jagged Array in Java
A two-dimensional array with different size of column in row is called jagged array in java.
- It in multi -dimensional array where no of coloumn in each row can be different .
int[ ][ ] arr = {
{11, 12, 13, 14, 15},
{7, 8, 9, 10},
{4, 5, 6},
{2, 3},
{1}
};
0 1 2 3 4
0 11 12 13 14 15
1 7 8 9 10
2 4 5 6
3 2 3
4 1
We can create a jagged array using the following syntax.
int[ ][ ] arr = new int[5][ ];
arr[0] = new int[5];
arr[1] = new int[4];
arr[2] = new int[3];
arr[3] = new int[2];
arr[4] = new int[1];
Now we can initialize values in jagged array like this:
arr[0][3] = 90;
arr[4][0] = 75;
Three Dimensional Array
An array with three indexes (subscripts) is called three dimensional array in java.
Ex :
int[ ][ ][ ] scores = new int[3][3][3];
Ex 2 :
int[ ][ ][ ] scores = {
{{75, 87, 69}, {90, 87, 85},{56, 67, 76}},
{{78, 67, 75}, {87, 98, 76}, {67, 56, 66}},
{{72, 63, 72}, {82, 91, 71}, {64, 56, 66}}
};
Comments
Post a Comment