Arrays
Normally, an array is a collection of similar types of elements that have contiguous memory locations.
Java array is an object the contains elements of a similar data type. It is a data structure where we store similar elements. We can store only a fixed set of elements in a java array.
Array in java is index-based, the first element of the array is stored at 0 indexes.
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.
The disadvantage of Java Array
● Size Limit: We can store the only fixed size of elements in the array. It doesn't grow its size at runtime. To solve this problem, a collection framework is used in java.
There are two types of array
● Single Dimensional Array● Multidimensional Array
Single Dimensional Array in java
● Syntax:
dataType[] arr; (or)
dataType []arr; (or)
dataType arr[];
Instantiation
● arrayRefVar=new datatype[size];
● int a[]={33,3,4,5};//declaration, instantiation and initialization
Multi-Dimensional Arrays
● In such a case, data is stored in a row and column-based index (also known as matrix form).
● Syntax
dataType[][] arrayRefVar; (or)
dataType [][]arrayRefVar; (or)
dataType arrayRefVar[][]; (or)
dataType []arrayRefVar[];
● Example
int[][] arr=new int[3][3];//3 row and 3 column
The foreach Loop
● JDK 1.5 introduced a new for loop known as for each loop or enhanced for loop, which enables you to traverse the complete array sequentially without using an index variable.
Advantage of the for-each loop:
● It makes the code more readable.
● It eliminates the possibility of programming errors.
Syntax of for-each loop:
● for(data_type variable : array | collection){}
Live Example(Just Run):-
class ForEachExample1
{
public static void main(String args[])
{
int arr[]={12,13,14,44};
for(int i:arr)
{
System.out.println(i);
}
}
}
Passing Arrays to Methods
Just as you can pass primitive type values to methods, you can also pass arrays to methods. For example, the following method displays the elements in an int arrayExample
public static void printArray(int[] array)
{
for (int i = 0; i < array.length; i++)
{
System.out.print(array[i] + " ");
}
}
You can invoke it by passing an array. For example, the following statement invokes the printArray method to display 3, 1, 2, 6, 4, and 2
Returning Arrays from Methods
● A method may also return an array. For example, the following method returns an array that is the reversal of another arrayExample
public static int[] reverse(int[] list)
{
int[] result = new int[10];
for (int i = 0, j = result.length - 1; i < list.length; i++, j--)
{
result[j] = list[i];
}
return result;
}
Variable Arguments
● Variable arguments can be used when the number of arguments that you want to pass to a method are not fixed.
● The method can accept different number of arguments of same type whenever they are called.
● Syntax returntype methodname (datatype...arrayname)
0 Comments