Array is set of values of similar type. Array elements share common name and array elements are stored in sequential memory locations. To refer to the elements of the array, we use indexes accordingly. Array indexing starts from “zero”. Predetermination of an array size is a must except in the case of giving values at the time of declaring an array.
Advantages of arrays:
· It is capable of storing many elements at a time
· It allows random accessing of elements i.e. any element of the array can be randomly accessed using indexes.
Disadvantages:
· Predetermining the size of the array is a must.
· There is a chance of memory wastage or shortage.
· To delete one element in the array, we need to traverse throughout the array.
· To delete one element in the array, we need to traverse throughout the array.
Creation of an array may involve three steps:
1. Declaring the array
2. Creating memory
3. Putting value into memory
Arrays can be of many types: single dimensional, multidimensional. Single dimensional array can represent either one row or one column of values. Whereas, multi-dimensional array can represent information in the form of rows and columns.
In Java, Single dimensional array can be declared as follows.
int a[ ] =new int[5];
Or int [] a= new int[5];
Or int a[] ={3,4,5,6,7};
In the above expressions, for the array ‘a’ 20 bytes of memory will be allocated and array ‘a’ can hold maximum of 5 integers at a time.
Array initialization can be done as follows:
int a[]={10,20,30,40,50}
to access the third element of the array we may use a[2] i.e. the index of third element in the array is 2.
To print the array, we may write:
for (i=0;i<5;i++)
System.out.println(a[i]);
Or, using for-each we may directly access the values
for(int x : a)
System.out.println(x);
In java, all arrays store the allocated size in a variable named length. We can obtain the length of the array a using a.length.
1 comments:
Thank you for the idea you shared about arrays. For programming The computer you are using needs to be fresh with its database and security. So it can provide you with the best service. Here the service pc reparatur Frankfurt form ITFux24 can assist you with every kind of solution for your pc or laptop. Contact today and get 10% off with first call!
Post a Comment