Lecture3-Jan18-ArraysString

Lecture3-Jan18-ArraysString - 1 1 / 53 COP 3503 SpRING 2012...

Info iconThis preview shows pages 1–16. Sign up to view the full content.

View Full Document Right Arrow Icon
1 / 53 COP 3503 SpRING 2012 Shayan Javed Lecture 3 Programming 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 / 53 Arrays Objects in Java (but not in C/C++). An array is a container object that holds a fixed number of values of a single type . Specify length when creating.
Background image of page 2
3 / 53 Arrays
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 / 53 Arrays Example: int array of length 5 . Declaration: int [] array1; Initialization: array1 = new int [5];
Background image of page 4
5 / 53 Arrays Length provided can be an int variable . Example: int x = 6; array1 = new int [x];
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 / 53 Arrays Two ways to declare: int [] array1; // convention int array1[]; // discouraged!
Background image of page 6
7 / 53 Arrays Declare and initialize with some values: int [] array1 = {5, 100, 200, 45}; Length = no. of elements between the braces.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 / 53 Arrays Property “length”. Very useful! Especially for traversing
Background image of page 8
9 / 53 Arrays Example: Printing out all the values of an array for ( int i = 0 ; i < array1.length ; i++ ) { System.out.println(array1[i]); }
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 / 53 Arrays Another way of traversing: for-each loop: for ( int i : array1 ) { System.out.println(i); } For each element i in array1 do…
Background image of page 10
11 / 53 Arrays What happens here? System.out.println(array1[ array1.length ]);
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
12 / 53 Arrays ArrayIndexOutOfBoundsException thrown Will later look at how to handle these exceptions. Be careful about boundaries!
Background image of page 12
13 / 53 Passing Arrays to Methods public static void printArray(int[] array) { for ( int i = 0 ; i < array.length ; i++ ) { System.out.println(array[i]); } } int [] array1 = {5, 100, 200, 45};
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
14 / 53 Passing Arguments to Methods Two ways: Pass-by-Value: Used for primitive types ( int/char/float/etc. ) Changing value in the method does not change the original variable Pass-by-reference: Used for objects (such as arrays). Changing in the method changes the original!
Background image of page 14
Pass-by-value
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 16
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/15/2012 for the course COP 3503 taught by Professor Staff during the Spring '12 term at University of Florida.

Page1 / 53

Lecture3-Jan18-ArraysString - 1 1 / 53 COP 3503 SpRING 2012...

This preview shows document pages 1 - 16. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online