CS2_10_Arrays_1D

CS2_10_Arrays_1D - CS2 Module 10 Category Elements of Java...

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

View Full Document Right Arrow Icon
CS2 Module 10 Category: Elements of Java Topic: Arrays (1D) Objectives Basic array concepts before OO!!!
Background image of page 1

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

View Full Document Right Arrow Icon
CS 2 Introduction to Object Oriented Programming Module 10 Elements of Java Arrays (1D)
Background image of page 2
Arrays of Primitives The Idea : Same concept as in Scheme (and most other computer languages) Scheme: (define grades (make-vector 10)) An array is a static structure. That is, once declared, its size or number of elements may not be changed A major advantage of arrays is direct random access to any element in O(1) time. In general arrays are homogeneous, that is each cell holds the same type of thing. This concept will be "stretched" when we come to polymorphism.
Background image of page 3

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

View Full Document Right Arrow Icon
Arrays of Primitives Java array variable declaration: <elemType> [ ] <arrID>; or <elemType> <arrID> [ ] ; Example int[ ] grades; int grades[]; float pressure[]; boolean[] statusFlags;
Background image of page 4
Arrays of Primitives Java array instantiation: <arrID> = new <elemType>[<size>]; Examples: grades = new int[10]; pressure = new float[100]; statusFlags = new boolean[15] Recall: int grades[]; float pressure[]; boolean[] statusFlags;
Background image of page 5

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

View Full Document Right Arrow Icon
Arrays of Primitives Java array variable declaration and instantiation: Preceding two steps may be combined into one! <elemType>[ ] <arrID> = new <elemType>[<size>]; or <elemType> <arrID>[ ] = new <elemType>[<size>]; e.g.: create an array called grades to store ten int s (used for storing 10 integer grades) int[ ] grades = new int[10]; or int grades[] = new int[10];
Background image of page 6
The odd looking syntax is because arrays are objects (even arrays of primitives). We'll explain in detail once we get to objects. .. Note the following are equivalent:
Background image of page 7

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

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 27

CS2_10_Arrays_1D - CS2 Module 10 Category Elements of Java...

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

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