Arrays
Arrays
Arrays:
type var-name[];
OR
type[] var-name;
Disadvantages:
Size Limit:
We can store only the fixed size of elements in the array. It doesn't grow its size
at runtime.
One Dimensional Array in java is always used with only one subscript
( [ ] ).
One Dimensional Array in java is always used with only one subscript (
[]).
A one-dimensional array behaves likes a list of variables.
You can access the variables of an array by using an index in square
brackets preceded by the name of that array.
Index value should be an integer. Before using the array, we must declare
it.
type var-name[];
OR
type[] var-name;
Example Program:1
class OnedimenisionaArray
{
public static void main(String[] args)
{
int[] arr; // declares an Array of integers.
arr = new int[5]; // allocating memory for 5 integers.
arr[0] = 10; // initialize the first elements of the array
arr[1] = 20; // initialize the second elements of the array
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
// accessing the elements of the specified array
for (int i = 0; i < arr.length; i++)
System.out.println("Element at index " + i + " : " + arr[i]);
}
}
Output:
Element at index 0 : 10
Element at index 1 : 20
Element at index 2 : 30
Element at index 3 : 40
Element at index 4 : 50
Multidimensional Arrays:
Multidimensional Arrays can be defined in simple
words as array of arrays. Data in multidimensional arrays are stored in tabular
form (in row major order).
data_type[1st dimension][2nd dimension][]..[Nth
dimension] array_name = new data_type[size1][size2]….[sizeN];
data_type: Type of data to be stored in the array. For example: int, char,
etc.
dimension: The dimension of the array created. For example: 1D, 2D, etc.
array_name: Name of the array
size1, size2, …, sizeN: Sizes of the dimensions respectively.
Two dimensional array:
2D array can be defined as an array of arrays. The
2D array is organized as matrices which can be represented as the collection of
rows and columns. The 2d array represents with two sub scripts.
Initializing 2D Arrays:
Output:
279
361
742
Multidimensional array:
Three – dimensional array is a complex form of a
multidimensional array. A three – dimensional array can be seen as an array of
two – dimensional array for easier understanding. Multi-dimensional array
represents with three subscripts. Elements in three-dimensional arrays are
commonly referred by x[i][j][k] where ‘i’ is the array number, ‘j’ is the row
number and ‘k’ is the column number.
Example:3
class MultiDimensional
{
public static void main(String[] args)
{
int[][][] arr = { { { 1, 2 }, { 3, 4 } },
{ { 5, 6 }, { 7, 8 } } };
Output:
12
34
56
78
Practice Programs:1
Output:
Original array: 1 2 3 4 5
Array in reverse order:
54321
Example:2
Output:
Elements of given array present on even position:
2
4
Example:3
public class OddPosition
{
public static void main(String[] args)
{
//Initialize array
int [] arr = new int [] {1, 2, 3, 4, 5};
System.out.println("Elements of given array present on odd position: ");
//Loop through the array by incrementing value of i by 2
for (int i = 0; i < arr.length; i = i+2)
{
System.out.println(arr[i]);
}
}
}
Output:
Elements of given array present on odd position:
1
3
5
Example:4
public class OddEvenInArrayExample
{
public static void main(String args[])
{
int a[]={1,2,5,6,3,2};
System.out.println("Odd Numbers:");
for(int i=0;i<a.length;i++)
{
if(a[i]%2!=0)
{
System.out.println(a[i]);
}
}
System.out.println("Even Numbers:");
for(int i=0;i<a.length;i++)
{
if(a[i]%2==0)
{
System.out.println(a[i]);
}
}
}
}
Output:
Odd Numbers:
1
5
3
Even Numbers:
2
6
2
Example: add two matrices
Output:
268
486
469
Example: multiply two matrices
Output:
6 6 6
12 12 12
18 18 18
Example: Transpose matrix
Output:
Printing Matrix without transpose:
134
243
345
Printing Matrix After Transpose:
123
344
435