Revision From Last Lecture - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Revision From Last Lecture

Description:

To group similar typed and related data for ease of your program usability ... The Good, the Bad & the Ugly. The Lord of the Rings: ROTK. The Godfather: Part II ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 20
Provided by: asim
Category:

less

Transcript and Presenter's Notes

Title: Revision From Last Lecture


1
Revision From Last Lecture
  • String functions
  • API referencing from sun java doc
  • Any questions?

2
Arrays
  • Why use arrays?
  • To group similar typed and related data for ease
    of your program usability
  • Extremely easy to use

3
Arrays (Contd)
  • So, an array
  • Is a sequence of related values
  • Has a user-specified fixed length
  • Is homogenous
  • Provides fast random access

4
Arrays (Contd)
  • We have to write a program which involves
    manipulation of large set of variables , say a
    list of movies
  • Are they related? (Although not a necessity)
  • Are they homogenous (list of strings?)

5
Arrays (Contd)
6
Arrays (Contd)
String moviesnew String10 movies0 The
Godfather movies1 The Shawshank
Redemption movies2 ... movies9
Star Wars Empire Strikes Back System.out.prin
tln(movies 1 )
7
Arrays (Contd)
  • // Example 2
  • public static void main (String args)
  • int A 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • // alternate way of declaring and defining an
    array.
  • // We can also use this method in our previous
    example. How?
  • for ( int i 0 i lt A.length i )
  • System.out.println( Ai )
  • Now we can also use length attribute in our
    previous example as well

8
Arrays and Functions Combined
  • // Example 3
  • public static int fibonacci( int n )
  • if ( n lt 1 ) return 1
  • int A new int n 1
  • A 0 A 1 1
  • for ( int i 2 i lt n i )
  • A i A i 1 A i 2
  • return A n
  • How does this function work?

9
Arrays Review of Last Lecture
  • We focused on
  • Array Declarations
  • Inserting values in arrays
  • Getting values out of arrays

10
Arrays as Parameters and Return Values in Methods
  • Array parameters and return values
  • Are passed by reference i.e. are not copied
  • They can be modified inside functions

11
Example 1
  • public static void main(String args)
  • int numbers 1,2,3,4,5
  • printdouble(numbers)
  • for (int i 0 i lt numbers.length i)
  • System.out.println( numbersi )
  • public static void printdouble(int nums)
  • for ( int j 0 j lt nums.length j )
  • nums j 2
  • System.out.println( nums j )

12
Example 2
  • public static int findMean( int A )
  • int sum 0
  • for ( int i 0 i lt A.length i )
  • sum A i
  • return sum / A.length

13
Example 3
  • public static void printPrimes( int n )
  • boolean isPrime new boolean n 1
  • for ( int i 1 i lt n i ) isPrimei
    true
  • for ( int d 2 d lt n d )
  • for ( int dd 2d dd lt n dd d )
  • isPrime dd false
  • for ( int i 1 i lt n i )
  • if ( isPrime i ) System.out.println( i )

14
Example 4
  • public static void main(String args)
  • int sum 0
  • for ( int i 0 i lt args.length i )
  • sum Integer.parseInt(argsi)
  • System.out.println( The sum sum )

15
Example 5
  • Refer to the CS7 site

16
2-Dimensional Arrays
  • A 2-d array
  • Is often known as Matrix
  • Defined with int M new introwscolumns
  • Often accessed with pair of nested FOR loops

17
2-D Array An Illustration
M
M00 M01 M02
M0col-1
M10 M11 M12
M1col-1
M20 M21 M22
M2col-1
Mrow-10 Mrow-11
Mrow-1col-1
18
Example
  • public static void main( String args )
  • int M new int 6 3
  • for (int row 0 row lt M.length row)
  • for (int col0colltM0.lengthcol)
  • Mrowcol row col
  • for (int row0 row lt M.length row)
  • for (int col0colltMrow.lengthcol)
  • System.out.print(Mrowcol )
  • System.out.println()

19
Illustration of the last example
M63
M00 M01 M02
M10 M11 M12
M20 M21 M22
M30 M31 M32
M40 M41 M42
M50 M51 M52
Matrix
Write a Comment
User Comments (0)
About PowerShow.com