Download presentation
Presentation is loading. Please wait.
1
Objects and Collections of Objects COMP53 Aug 31 2007
2
Classes and Objects In Java, (almost) everything is either a class or an object. A class defines a type, which describes how to create and use objects which are instances of that class. Class members (non-static): – Instance variable or fields – Methods
3
Static Class Members Classes may also define static members, one copy of which is shared by all instances of the class This makes the class an object itself, which holds the static/shared members Static members can be accessed and used even when no instances of the class exist
4
References Java is a referenced based language. This means that names do not refer directly to objects. Names are indirect references to objects. This distinction will be very important as we start building data structures.
5
// george is the name of a reference to a Chimpanzee Chimpanzee george; // create a Chimpanzee object and set george // to refer to that object. george = new Chimpanzee(); // galen is now a reference to the same object Chimpanzee galen = george; References: Example george Chimpanzee object george Chimpanzee object galen
6
Collections We often need to manage a large number of objects We do this by creating collection objects, which are structures that can hold multiple values or references. Most collection classes maintain special rules on how the collection is stored and accessed. For example: a queue collection only allows you to access the value that has been in the collection the longest (the head of the queue).
7
Arrays A common collection is the array An array is an indexed sequence of values The indexes are integers, beginning with zero
8
// a is a reference to an array of integers int[ ] a; // the array object has space to hold 5 integers a = new int[5]; // the first element of the array now holds a 5 a[0] = 5; // the fourth element of the array now holds a 7 a[3] = 5; Array of Integers a a a 5 a 57
9
To store objects, we can use an array of object references Creating the array only creates the references, objects must be individually created. Chimpanzee[ ] c;// a is a reference to an array of Chimpanzee references a = new Chimpanzee[5];// the array has space to hold 5 Chimpanzee references // note: We still have not created any Chimpanzees Storing Objects c c
10
After the array is created, then we can add object references to it c[0] = new Chimpanzee(“George”, 180.0); c[0] = new Chimpanzee(“Galen”, 150.0); Storing Objects (continued) c “George” 180.0 c “Galen” 150.0
11
When moving object references, take care to not lose any of the objects. Suppose we want to swap the objects references by x and y: We might try: y = x; x = y; But then we’ll lose Object 2. Juggling References xy Object 1Object 2 xy Object 1Object 2
12
Use a temporary reference to hold references when necessary: temp = y; y = x; x = temp; Juggling References (cont.) xy Obj1Obj2 tempxy Obj1Obj2 temp xy Obj1Obj2 temp xy Obj1Obj2 temp
13
When moving references in an array (or other data structure) we also need temporary references // swap second and third objects in array a temp = a[2]; a[2] = a[1]; a[1] = temp; Moving References in an Array
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.