Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays of Objects.

Similar presentations


Presentation on theme: "Arrays of Objects."— Presentation transcript:

1 Arrays of Objects

2 String[] words = new String[25];
Arrays of Objects The elements of an array can be object references The following declaration reserves space to store 25 references to String objects String[] words = new String[25]; It does NOT create the String objects themselves Each object stored in an array must be instantiated separately An array can contain objects, and each of those objects could have several variable and the methods that use them. When we store objects in an array, each element is a separate object. That is, an array of objects is really an array of object references.

3 GradeRange.java The GradeRange program creates an array of String objects called grades, which stores the possible grades and their numeric lowest value, and then prints them. For example, the lowest grade you can have to get an A is 95. Sometimes two arrays with corresponding elements like this are called parallel arrays. Parallel arrays can be tricky because they can get out of synch with each other.

4 Arrays of Objects Objects can have arrays as instance variables
Many useful structures can be created with arrays and objects The software designer must determine carefully an organization of data and objects that makes sense for the situations

5 Tunes.java It is important to remember that creating an array and creating the objects that are stored in the array are two separate steps. In the Tunes program we see these two steps. The Tunes class contains a main method that creates, changes and looks at a CD collection. Each CD added to the collection has a title, artist, purchase price, and number of tracks.

6 CDCollecton.java The CDCollection class contains an array of CD objects representing the collection. It counts the CDs in the collection and their combined value. It also keeps track of the size of the collection array so that a larger array can be created if too many CDs are added to the collection. The collection array is instantiated in the CD Collection constructor. Every time a CD is added to the connection, using the addCD method, a new CD object is crated and a reference to it is stored in the collection array. Each time a CD is added to the collection, we check to see whether we have filled up the collection array. If we didn’t check this, we would get an exception when we tried to store a new CD object at an invalid index. If the array is full, the private increaseSize method is invoked, which first creates an array that is twice the as big as the current collection array. Each CD in the collection is then copied into the new array. Finally, the collection reference is set to the larger array. This means we never run out of room in our CD collection. The user of the CDCollection object, the main method, never has to worry about running out of space because it is all handle internally.

7 CD.java The toString method of the CDCollection class returns a report summarizing the collection. The report is created, in part, using calls to the toString method of each CD object stored in the collection. The CD class contains the CD constructor as well as the toString method.

8


Download ppt "Arrays of Objects."

Similar presentations


Ads by Google