Page 349 - CITS - Computer Software Application -TT
P. 349

COMPUTER SOFTWARE APPLICATION - CITS



            JAVA Arrays

           Normally, an array is a collection of similar type of elements which has contiguous memory location
           Java array is an object which contains elements of a similar data type. Additionally, The elements of an array are
           stored in a contiguous memory location. 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 the
           0th index, 2nd element is stored on 1st index and so on.

           Unlike C/C++, we can get the length of the array using the length member. In C/C++, we need to use the sizeof
           operator.
           In Java, array is an object of a dynamically generated class. Java array inherits the Object class, and implements
           the Serializable as well as Cloneable interfaces. We can store primitive values or objects in an array in Java. Like
           C/C++, we can also create single dimensional or multidimensional arrays in Java. Moreover, Java provides the
           feature of anonymous arrays which is not available in C/C++.













           Advantages
           Code Optimization: It makes the code optimized, we can retrieve or sort the data efficiently.
           Random access: We can get any data located at an index position
           Disadvantages

           Size Limit: We can store only the fixed size of elements in the array. It doesn’t grow its size at runtime. To solve
           this problem, collection framework is used in Java which grows automatically.
           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];
           Example of Java Array
           Let’s see the simple example of java array, where we are going to declare, instantiate, initialize and traverse an
           array.
           Java Program to illustrate how to declare, instantiate, initialize
           and traverse the Java array.
           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;





                                                           336

                              CITS : IT&ITES - Computer Software Application - Lesson 85 - 93
   344   345   346   347   348   349   350   351   352   353   354