An array is a container object that holds a fixed number of
values of a single type. The length of an array is established when the
array is created. After creation, its length is fixed. You have seen an
example of arrays already, in the
main
method of the "Hello World!" application. This section discusses arrays in greater detail.
An array of 10 elements.
Arrays are basically memory location ,it will store the multiple Data,it has the index,
*Some drawbacks of Array
1)It is fot particular DataTypes
2)Size of the Array
int[i]=new int[10]//Advantage of the Array is Length of the Array
Object Array :Object array is a inbuilt class it is super class of the class
It can Store Different type of DataTypes
Object ar[] = new Object[4];
ar[0] = "Hello"
ar[1]="1231"
ar[2]="true"
ar[3]="32.21"
Simple Programm for Array
public class TestArray { public static void main(String[] args) { double[] myList = {1.9, 2.9, 3.4, 3.5}; // Print all the array elements 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 is " + 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 is " + max); } }
No comments:
Post a Comment