• Save
Md05 arrays
Upcoming SlideShare
Loading in...5
×
 

Md05 arrays

on

  • 557 views

 

Statistics

Views

Total Views
557
Views on SlideShare
557
Embed Views
0

Actions

Likes
1
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Md05 arrays Md05 arrays Presentation Transcript

  • 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