9-20101101 - Introduction to OO Program Design Software...

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

View Full Document Right Arrow Icon
Introduction to OO Program Design Software College of SCU Instructor: Shu Li Email: [email protected]
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 Unit 2.2 Collections 2.2.1 Arrays 2.2.2 Vectors and Iterators 2.2.3 Implementing the Collections of the Library System 2.2.4 Collection Framework
Background image of page 2
3 What is a Collection? A “collection” object — sometimes called a container — is simply an object that groups multiple elements into a single unit Collections are used to store, retrieve, manipulate, and communicate aggregate data Typically, they represent data items that form a natural group, such as a poker hand (a collection of cards), a mail folder (a collection of letters), or a telephone directory (a mapping of names to phone numbers).
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 Unit 2.2 Collections 2.2.1 Arrays 2.2.2 Vectors and Iterators 2.2.3 Implementing the Collections of the Library System 2.2.4 Collection Framework
Background image of page 4
5 Array Group data objects of the same type element o [] J j 0 o Declare arrays char ch[]; or char[] ch; Create space for a reference Remember an array is an object not memory reserved for primitive types
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 Creating an Array Use the new keyword to create an array object The statement in the sample program allocates an array with enough memory for ten integer elements and assigns the array to the variable anArray declared earlier. new dataType[arraySize] int[] anArray = new int[10];
Background image of page 6
7 Accessing an Array Element Now that some memory has been allocated for the array, the program assign values to the array elements: Note that in Java, array indices begin at 0 and end at the array length minus 1. for (int i=0; i<anArray.length; i++) { anArray[i] = i; System.out.print(anArray[i] + " "); }
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 Example Array class ArrayDemo { public static void main(String[] args) { // declare an array of integers and create an array of integers int[] anArray = new int[10]; // assign a value to each array element and print for (int i=0; i< anArray.length; i++) { anArray[i] = i; System.out.print(anArray[i] + " "); } System.out.println(); } }
Background image of page 8
9 Initializing an array Java provides a shortcut syntax for creating and initializing an array. int[] x = { 1, 3, 5, 7, 9 }; String[] strDays = { “Mon, ”Wed”, ”Fri” }; boolean[] answers = { true, false, true, true };
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 Arrays Summary The size of an array is fixed at the time it is created and can not be changed afterwards. Any attempt to access an element outside the length of an array will throw an ArrayIndexOutOfBoundsException. All the elements stored in the array must be of the same type or class. An array has a public attribute called length which indicates the number of elements that it contains. The elements contained in an array can also be arrays! (And the elements in those arrays can be arrays, and so on …). These are known as multi-dimensional
Background image of page 10
11 Example Using Arrays Dictionary.java
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 Unit 2.2 Collections 2.2.1 Arrays 2.2.2 Vectors and Iterators 2.2.3 Implementing the Collections of the Library System 2.2.4 Collection Framework
Background image of page 12
13 java.util.Vector Package java.util The Vector class implements a growable array of objects.
Background image of page 13

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

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

This note was uploaded on 06/12/2011 for the course ECON 101 taught by Professor Professor during the Spring '10 term at Cisco Junior College.

Page1 / 70

9-20101101 - Introduction to OO Program Design Software...

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

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