Java Technology 2018 - Array
Array:
Array is a collection of similar type of elements that have contiguous memory location. Array in java is index based, first element of the array is stored at 0 index.
Array is a collection of similar type of elements that have contiguous memory location. 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.
There are two types of array.
- Single Dimensional Array
- Multidimensional Array
Syntax to Declare an Array in java
dataType[] arr; (or)
dataType []arr; (or)
dataType arr[];
Instantiation of an Array in java
arrayRefVar=new datatype[size];
class Testarray{
public static void main(String args[]){
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//printing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}}
int a[]={33,3,4,5};//declaration, instantiation and initialization
package com.practice;
public class ArrayPractice {
public static void main(String[] args) {
int arr[] = {22,11,44,33};
for(int i:arr){
System.out.println(i);
}
}
}
package com.practice;
public class ArrayFindMinimum {
public static void main(String[] args) {
int arr[] = { 22, 11, 55, 44 };
findMin(arr);
}
public static void findMin(int[] arr) {
int min=arr[0];
for(int i: arr){
if(i<min){
min=i;
}
}
System.out.println(min);
}
}
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
dataType[][] arrayRefVar; (or)
dataType [][]arrayRefVar; (or)
dataType arrayRefVar[][]; (or)
dataType []arrayRefVar[];
Example to instantiate Multidimensional Array in java
int[][] arr=new int[3][3];//3 row and 3 column
Java String array: is there a size of method?
Yes,
.length
(property-like, not a method):String[] array = new String[10];
int size = array.length;
Getting the array length of a 2D array in Java
If you're treating your 2D array as a matrix, you can simply get z.length and z[0].length, but note that you might be making an assumption that for each array in the 2nd dimension that the length is the same (for some programs this might be a reasonable assumption).
package com.practice;
public class ArrayTwoD {
public static void main(String[] args) {
int arr[][] = { { 1, 2 }, { 44, 3 }, { 8, 5 } };
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
int arr1[] = { 0, 1, 2, 3, 4, 5 };
int arr2[] = { 5, 10, 20, 30, 40, 50 };
System.arraycopy(arr1, 0, arr2, 1, 3);
for (int i : arr2) {
System.out.println(i);
}
3 elemens from arr1 index 0 to arr2 index 1
5
0
1
2
40
50
int arr1[] = { 0, 1, 2, 3, 4, 5 };
int arr2[] = { 5, 10, 20, 30, 40, 50 };
System.arraycopy(arr1, 1, arr2, 1, 3);
for (int i : arr2) {
System.out.println(i);
}
3 elemens from arr1 index 1 to arr2 index 1
5
1
2
3
40
50
Comments
Post a Comment