CS2_18_OOArrays

CS2_18_OOArrays - CS2 Module 18 Category: OO Concepts...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: CS2 Module 18 Category: OO Concepts Topic: OO Implementation of Arrays Objectives Full treatment of arrays CS 2 Introduction to Object Oriented Programming Module 18 OO Concepts OO Implementation of Arrays Arrays Arrays The Array itself is an O bject , even if the array contains primitives. (Array identifier references an object). This is unlike many other computer languages! An array may contain elements that are: primitives , or objects (actually references!!!) . Arrays If an array of objects , then: the array identifier is a reference to the array object each array element is a reference to an object of the class specified as the element type for the array Instantiating the array object does not instantiate the various element objects to which it refers. Element objects must be explicitly instantiated and initialized. Example : Imagine a Flatware object which keeps track of: knives, forks and spoons . class Flatware { int knives = 0; int forks = 0; int spoons = 0; public Flatware(int knives, int forks, int spoons) { setKnives(knives); setForks(forks); setSpoons(spoons); } // of constructor /* We assume accessors, modifiers--getKnives(), setKnives(), etc. -- are implemented here */ } // of Flatware Arrays Arrays Then, the code segment: Flatware fw1 = new Flatware(10, 20, 30); Flatware fw2; produces: knives = 10 forks = 20 spoons = 30 fw1 fw2 Arrays Arrays and the code segment: Flatware[ ] flatwareArray = new Flatware[5]; produces: which could then be initialized via: int i; for ( i = 0; i < flatwareArray.length; i++)for ( i = 0; i < flatwareArray....
View Full Document

Page1 / 24

CS2_18_OOArrays - CS2 Module 18 Category: OO Concepts...

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

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