Download presentation
Presentation is loading. Please wait.
Published byAlexandra Victoria Scott Modified over 9 years ago
1
Collections behaviour and implementation of collections of primitives and objects
2
D. Goforth, COSC 3106, fall 20032 Arrays – of primitive types arrays are objects array variables contain references to arrays int[] x = new int[5]; x[2] = 550; // indexing starts at 0 x 0005500
3
D. Goforth, COSC 3106, fall 20033 Arrays – of objects arrays of object types array variables contain references to arrays of references String[] y = new String[5]; y[2] = “Hello”; y Hello
4
D. Goforth, COSC 3106, fall 20034 Arrays accessing individual elements find size of array with length variable int[] x = new int[5]; x[2] = 550; int number = x.length;//number contains 5 index range: 0 to length-1 x[0] = 100; x[4] = -20; x[5] = 1000; // ArrayIndexOutOfBoundsException thrown
5
D. Goforth, COSC 3106, fall 20035 Partly filled arrays problem with collection of items that changes in size eg list of paddleboats for rental inventory is 10 boats but number available varies between 0 and 10 use array of integers to store identity numbers of boats available
6
D. Goforth, COSC 3106, fall 20036 Partly filled arrays array length is fixed data is always at front of array use an integer counter to track how many data items are in the array x 701680550-320 xCount 4 00050109
7
D. Goforth, COSC 3106, fall 20037 Partly filled arrays when new data is added to next location, count is increased x 70168550-320 xCount 5 00050109 x 701680550-320 xCount 4 00050109 112
8
D. Goforth, COSC 3106, fall 20038 Partly filled arrays example – BoatCollection inventory collection of boats that are rented out and returned BoatCollection.html BoatCollection.java
9
D. Goforth, COSC 3106, fall 20039 Multi-dimensional arrays to represent matrices, tables, calendar months, patch arrays e.g., distance chart Sudbury0 Toronto1 Ottawa2 Sudbury Toronto Ottawa 0 12 0400500 4000600 5006000
10
D. Goforth, COSC 3106, fall 200310 Multi-dimensional arrays int[][] m = new int[3][4]; m[2][3] = -60; 012012 0 12 3 000 000 000 0 0 -60
11
D. Goforth, COSC 3106, fall 200311 Review of some array operations first question: entire array or ‘real’ data subset? sum of data maximum value range of data increment every value search for value
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.