Download presentation
Presentation is loading. Please wait.
Published byMelvin Justice Modified over 9 years ago
1
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham
2
Week 10 Topics 10.1.1 Wrappers and Auto-boxing 10.1.2 The Enhanced for Loop 10.1.3 Simple Array Algorithms 10.1.4 Two-dimensional Arrays 10.1.5 Copying Arrays
3
10.1.1 Wrappers and Auto-boxing Because numbers are not objects in Java, you cannot directly enter them in array lists For example, this will not compile: ArrayList data = new ArrayList (); // No To store sequences of numbers in an array list, you must turn them into wrapper classes
4
10.1.1 Wrappers and Auto-boxing Cont. Primitive TypeWrapper Class byteByte booleanBoolean charCharacter doubleDouble floatFloat intInteger longLong shortShort
5
10.1.1 Wrappers and Auto-boxing Cont. Beginning with Java 5.0, if you assign a primitive type to a wrapper class, the conversion is automatic (called auto-boxing): Double d = 29.95; Conversely, beginning with Java 5.0, wrapper classes are automatically converted to primitive types (called auto-unboxing): double x = d;
6
10.1.1 Wrappers and Auto-boxing Cont. This will work to store simple number types in an array list: ArrayList data = new ArrayList (); // Yes data.add(29.95); double x = data.get(0);
7
10.1.2 The Enhanced for Loop Java version 5.0 introduces a very convenient shortcut for a common loop type, when you need to iterate through an sequence of elements The new loop construct is know as the “for each” loop The “for each” loop has a very specific purpose, traversing the elements of a collection from the beginning to the end
8
10.1.2 The Enhanced for Loop Cont. double[] data = new double[100]; … assume that array is then populated double sum = 0; for (double e : data) { sum = sum + e; }
9
10.1.2 The Enhanced for Loop Cont. ArrayList accounts = new ArrayList (); … assume that array list is then populated double sum = 0; for (BankAccount a : accounts) { sum = sum + a.getBalance(); }
10
10.1.3 Simple Array Algorithms Counting Matches Finding a Value (linear search) Finding the Maximum or Minimum
11
10.1.3 Simple Array Algorithms Cont. Counting Matches double atLeast = 5000.00; int matches = 0; for (BankAccount a : accounts) { if (a.getBalance() >= atLeast) matches++; } System.out.println(matches);
12
10.1.3 Simple Array Algorithms Cont. Finding a Value (linear search) int accountNumber = 1001; for (BankAccount a : accounts) { if (a.getAccountNumber() == accountNumber) { System.out.println (a.getBalance()); break; } }
13
10.1.3 Simple Array Algorithms Cont. Finding the Maximum or Minimum BankAccount max = accounts.get(0); for (int i = 1; i < acocunts.size(); i++) { BankAccount a = account.get(i); if (a.getBalance() > max.getBalance()) max = a; } System.out.println(max.getAccountNumber());
14
10.1.4 Two-Dimensional Arrays int[][] table = new int[2][3]; The array identified by table will have 2 rows (the row is the FIRST subscript) and 3 columns (the column is the SECOND subscript). [0][0][0][1][0][2] [1][0][1][1][1][2]
15
10.1.4 Two-Dimensional Arrays Cont. table[0][0] = 22; table[0][1] = 1301;... table[1][2] = 43 // Traversing the 2-D array for (int i = 0; i < 2; ++i) // rows for (int j = 0; j < 3; ++j) // cols System.out.println(table[i][j];
16
10.1.5 Copying Arrays Array variables work just like object variables, they hold a reference to the actual array If you copy the reference, you get another reference to the same array: double[] data = new double[10]; … // fill array double[] prices = data;
17
10.1.5 Copying Arrays Cont. If you want to make a true copy of an array, call the clone method Note that you need to cast the return value of the clone method from the type Object to the array type: double[] prices = (double[]) data.clone();
18
10.1.5 Copying Arrays Cont. Use the static System.arraycopy method to copy elements from one array to another System.arraycopy(from, fromStart, to, toStart, count) To add a new element at position i into data: System.arraycopy(data, i, data, i+1, data.length –i -1); data[i] = x;
19
10.1.5 Copying Arrays Cont. To grow an array that has run out of space: double[] newData = new double[2 * data.length); System.arraycopy(data, 0, newData, 0, data.length); data = newData;
20
Reference: Big Java 2 nd Edition by Cay Horstmann 10.1.1 Wrappers and Auto-boxing (section 8.3 in Big Java) 10.1.2 The Enhanced for Loop (section 8.4 in Big Java) 10.1.3 Simple Array Algorithms (section 8.5 in Big Java) 10.1.4 Two-dimensional Arrays (section 8.6 in Big Java) 10.1.5 Copying Arrays (section 8.7 in Big Java)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.