Module 5     Arrays  
 
  Arrays •  Java array is an ordered collection of primitives,  object references, or other arrays.   •  A structure that holds multiple values of  the same type. •  An array is an object; it is created with new
To create an array follow three steps Declaration  Construction Initialization
Declaration Declaration tell the array’s name and what type its  element will be. int[]ints;  //declare array of primitive types double[] dubs; Dimension[] dims;  //declare array of object reference Float[][]twodee; //declare a two dimensional array
Construction Notice the declaration doesnot specify the size of array, size is specified at runtime when array is allocated  via new keyword . int[] ints; ints=new int[25]; Use the new keyword to create an array object.
Declaration and construction performed in single  line as  int[] ints=new int[25];
Initialization When the array is constructed its elements are  automatically initialized to their default value Element type Initial Value byte 0 short 0 i nt   0 long 0L float 0.0f double 0.0d char ‘\u0000’ b oolean false object reference   null
T o initialize an array to values other than above  combine declaration,construction and initialization  into single step . Implicit assigning of values boolean[] answers = { true, false, true, true, false }; float[]diameters={1.1f,2.2f,3.3f,4.4f,5.5f};    Explicit assigning of values long[] squares; squares=new long[6000]; for(int i=0;i<6000;i++){ squares[i]=i*i; }
Accessing an Array Element for (int i = 0; i < anArray.length; i++) { anArray[i] = i; System.out.print(anArray[i] + &quot; &quot;);   }   Note: Java array’s index always start at 0.
Getting the Size of an Array arrayname.length Note :length is not a method. length is a property  provided by the Java platform for all arrays.
 
Example   ArrayOfArraysDemo
  Array Bounds All array subscripts begin at 0: int list[] = new int [10]; for (int i = 0; i < list.length; i++) { System.out.println(list[i]); }
  Array Resizing •  Cannot resize an array •  Can use the same reference variable to refer to an  entirely new array:  int  elements[]  =  new  int[6]; elements  =  new  int[10]; Example   ArrayDemo.java
Example  ArrayCopyDemo

Md05 arrays

  • 1.
    Module 5 Arrays  
  • 2.
  • 3.
    Arrays• Java array is an ordered collection of primitives, object references, or other arrays. • A structure that holds multiple values of the same type. • An array is an object; it is created with new
  • 4.
    To create anarray follow three steps Declaration Construction Initialization
  • 5.
    Declaration Declaration tellthe array’s name and what type its element will be. int[]ints; //declare array of primitive types double[] dubs; Dimension[] dims; //declare array of object reference Float[][]twodee; //declare a two dimensional array
  • 6.
    Construction Notice thedeclaration doesnot specify the size of array, size is specified at runtime when array is allocated via new keyword . int[] ints; ints=new int[25]; Use the new keyword to create an array object.
  • 7.
    Declaration and constructionperformed in single line as int[] ints=new int[25];
  • 8.
    Initialization When thearray is constructed its elements are automatically initialized to their default value Element type Initial Value byte 0 short 0 i nt 0 long 0L float 0.0f double 0.0d char ‘\u0000’ b oolean false object reference null
  • 9.
    T o initializean array to values other than above combine declaration,construction and initialization into single step . Implicit assigning of values boolean[] answers = { true, false, true, true, false }; float[]diameters={1.1f,2.2f,3.3f,4.4f,5.5f};  Explicit assigning of values long[] squares; squares=new long[6000]; for(int i=0;i<6000;i++){ squares[i]=i*i; }
  • 10.
    Accessing an ArrayElement for (int i = 0; i < anArray.length; i++) { anArray[i] = i; System.out.print(anArray[i] + &quot; &quot;);   }   Note: Java array’s index always start at 0.
  • 11.
    Getting the Sizeof an Array arrayname.length Note :length is not a method. length is a property provided by the Java platform for all arrays.
  • 12.
  • 13.
    Example ArrayOfArraysDemo
  • 14.
    ArrayBounds All array subscripts begin at 0: int list[] = new int [10]; for (int i = 0; i < list.length; i++) { System.out.println(list[i]); }
  • 15.
    ArrayResizing • Cannot resize an array • Can use the same reference variable to refer to an entirely new array: int elements[] = new int[6]; elements = new int[10]; Example ArrayDemo.java
  • 16.