10 Collection of Data
10 Collection of Data
Programming
Collection of Data
Acknowledgement
The contents of these slides have origin from
School of Computing, National University of
Singapore.
We greatly appreciate support from Mr. Aaron
Tan Tuck Choy, and Dr. Low Kok Lim for
kindly sharing these materials.
2
Policies for students
These contents are only used for students
PERSONALLY.
Students are NOT allowed to modify or
deliver these contents to anywhere or anyone
for any purpose.
3
Objectives
Using arrays
Book
• Array: Chapter 1, Section
1.1, pages 35 to 38
• Generics: Chapter 9, Section
9.4, pages 499 to 507
String objects
We can use this for command-line arguments
public class TestCommandLineArgs { TestCommandLineArgs.java
public static void main(String[] args) {
for (int i=0; i<args.length; i++)
System.out.println("args[" + i + "] = " + args[i]);
}
}
Array
5
2
1
1.
7 3
1
5
public class Test2DArray {
6
public static void main(String[] args) {
int[][] array2D = { {4, 5, 2}, {1, 3}, {7, 1, 5, 6} };
System.out.println("array2D.length = " + array2D.length);
for (int i = 0; i < array2D.length; i++)
System.out.println("array2D[" + i + "].length = "
+ array2D[i].length);
for (int row = 0; row < array2D.length; row++) {
for (int col = 0; col < array2D[row].length; col++)
System.out.print(array2D[row][col] + " ");
System.out.println(); array2D.length = 3
}
array2D[0].length = ?
}
}
array2D[1].length = ?
Test2DArray.java array2D[2].length = ?
?
?
?
[503005 Lecture 10: Collection of Data]
22
Drawback
Array
Array has one major drawback:
1.
type declarations
In C, there is no easy way to exploit the similarity:
You need a separate implementation for each data
type
In Java, you can make use of generic
programming:
A mechanism to specify solution without tying it
down to a specific data type
do {
System.out.printf("Enter a number in (%d to %d): ",
range.getFirst(), range.getSecond());
input = sc.nextInt();
}
[503005 Lecture 10: Collection of Data]
28
The Generic Pair Class
Generics
class Pair <T> {
private T first, second;
2.
public Pair(T a, T b) {
first = a;
second = b;
}
public T getFirst() { return first; }
public T getSecond() { return second; }
}
Pair.java
Important restriction:
The generic type can be substituted by reference data type only
Hence, primitive data types are NOT allowed
Need to use wrapper class for primitive data type
int a = 10;
true
Integer b = 10; // autoboxing
System.out.println(a == b);
public NewPair(S a, T b) {
first = a;
second = b;
}
public S getFirst() { return first; }
public T getSecond() { return second; }
}
NewPair.java
Dynamic size
expands or shrinks automatically
Generic
allows any reference data types
Useful predefined methods
Use array if the size is fixed; use Vector if the
size may change.
import java.util.Vector;
3.
Vector<E> myVector;
int size()
Returns the number of components in this vector.
if (courses.contains("503005"))
System.out.println("503005 is in courses");
courses.remove("503005");
for (String c: courses)
System.out.println(c); The enhanced for-loop is applicable
} to Vector objects too!
}
[503005 Lecture 10: Collection of Data]
41
4 ArrayList class
ArrayList<E> myArrayList;
int size()
Returns the number of elements in this list.
Array:
- Declaration and common usage
Java Elements
Generics:
- Allowing operation on objects of various types
following values:
The number of unique random integers to generate; and
Limit of the values: each random number generated should be in
the range from 0 (inclusive) to limit (exclusive), or [0, limit – 1].
(Certainly, the second input value must not be smaller than the first)
Each time a random integer is generated, you must
check if it is a duplicate of an earlier generated value. If it
is, it must be discarded. The program goes on to
generate the required number of unique random
integers.
You are to count how many duplicates were detected.
[503005 Lecture 10: Collection of Data]
53
Detecting Duplicates (2/4)
Sample run
(In testing your code, each time a random number is
Additional
computation is correct)
Enter number of unique integers to generate: 10
Enter limit: 20
List: [16, 3, 15, 17, 2, 10, 18, 5, 12, 14]
Duplicates detected: 8