Md05 arrays

599 views
509 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
599
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Md05 arrays

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

×