Core Java - ArrayInstead of declaring individual variables, such as number0, number1, ..., and number99,etc., you can declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99],etc., to represent individual variables. Declaring Array Variables : To use an array in a program, you must declare a variable to reference the array, and you must specify the type of array the variable can reference. Syntax: dataType[] arrayRefVar;Eg: double[] myList;Creating Arrays: You can create an array by using the new operator with the following syntax: arrayRefVar =new dataType[arraySize];The above statement does two things: Declaring an array variable, creating an array, and assigning the reference of the array to the variable can be combined in one statement, as shown below: dataType[] arrayRefVar =new dataType[arraySize];Alternatively you can create arrays as follows: dataType[] arrayRefVar ={value0, value1,..., valuek};The array elements are accessed through the index. Array indices are 0-based; that is, they start from 0 to arrayRefVar.length-1. Eg: Following statement declares an array variable, myList, creates an array of 10 elements of double type and assigns its reference to myList: double[] myList =new double[10];Following picture represents array myList. Here, myList holds ten double values and the indices are from 0 to 9. ![]() Processing Arrays: When processing array elements, we often use either for loop or foreach loop because all of the elements in an array are of the same type and the size of the array is known. Eg: public class Test{ public static void main(String[] args){ double[] myList ={1.9,2.9,3.4,3.5}; // Print all the array elements System.out.println("List of array: "); for(int i =0; i < myList.length; i++){ System.out.println(+myList[i]+" "); } // Summing all elements double total =0; for(int i =0; i < myList.length; i++){ total += myList[i]; } System.out.println("Total :"+ total); // Finding the largest element double max = myList[0]; for(int i =1; i < myList.length; i++){ if(myList[i]> max) max = myList[i]; } System.out.println("Max : "+ max); } }Output : List of array: 1.9 2.9 3.4 3.5 Total : 11.7 Max : 3.5 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 array: 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 eg, the following statement invokes the printArray method to display 3, 1, 2, 6, 4, and 2: printArray(newint[]{3,1,2,6,4,2});Returning an Array from a Method: A method may also return an array. For eg, the method shown below returns an array that is the reversal of another array: public static int[] reverse(int[] list){ int[] result =newint[list.length]; for(int i =0, j = result.length -1; i < list.length; i++, j--){ result[j]= list[i]; } return result; }Types of Array :
1. Single Dimensional Array : Syntax : datatype arr[];Eg: public class TestArray{ public static void main(String [] args){ System.out.println("Welcome to Single Dimensional Array"); int ar[]= new int[]; a[0] = 10; a[1] = 20; a[2] = 30; a[3] = 40; a[4] = 50; for(int i=0; i < a.length; i++)//length is the property of array System.out.println(a[i]); } } Output : Welcome to Single Dimensional Array 10 20 30 40 50 2. Multidimensional Array : Syntax: dataType arrayRefVar[][];Eg: class Testarray3{ public static void main(String args[]){ int arr[][]={{1,2,3},{2,4,5},{4,4,5}}; for(int i=0;i < 3; i++){ for(int j=0;j < 3; j++){ System.out.print(arr[i][j]+" "); } System.out.println(); } } }Output : 1 2 3 2 4 5 4 4 5 ☛ Join to Learn from Experts: TesDBAcademy - Best Java Institute in Chennai
« Previous
Next Topic »
(Core Java - Collection)
|