We have examined in passing the use of vectors ( Before we begin, you should ensure that you are familiar with the standard Java arrays. To check that you are familiar I would ask you to solve this very short problem yourself: Write a Java application that reads in a number of strings from the command line and provides a histogram of the numbers of letters in the words passed e.g. if the program was executed as: java ArrayApp This is a test string it would return 1 word with 1 letter, 1 words with 2 letters, 2 words with 4 letters etc. The solution to this problem is at the bottom of the page. We have seen how to sort an array in C++ using STL. In Java, there is a helper class for working with arrays called import java.util.Arrays; public class SortArrays { public static void main(String[] args) { Arrays.sort(args); for(int i=0; i<args.length; i++) { System.out.println(args[i]); } } } Which will execute as: C:\temp\java SortArrays Dog Bird Cat Apple Apple Bird Cat Dog It is therefore quite straightforward to sort Strings and indeed int, float etc. arrays. It gets more complicated when we wish to sort objects of our own type. You will remember that in C++ we had to override the operators that operated on the data type (e.g. equals, less than, more than). In Java we have to do pretty much the same thing, but since we have no operator overloading we must use interfaces, the Suppose we have a straightforward
We can provide comparisons of the states of this class by implementing the
Where the comparison returns -1 in the case where the left object is less than the right object, +1 in the opposite case and 0 when the two objects are numerically identical In the case of comparing two strings it is fairly straightforward as we can just use the
Finally, we write the application code to use our comparators with our data type:
On execution this will give the output: The students sorted by name: A student with name: Adam and id 1233 A student with name: Derek and id 1234 A student with name: Jane and id 1231 A student with name: Mick and id 1237 A student with name: Sarah and id 1238 The students sorted by id: A student with name: Jane and id 1231 A student with name: Adam and id 1233 A student with name: Derek and id 1234 A student with name: Mick and id 1237 A student with name: Sarah and id 1238 As you can imagine it is possible to make quite advanced comparisons between objects of the class that you define. I have given the example before of creating a Generics in Java provides us with a better way. We can create a typed container of elements. For example, we could create a List<Student> studentList = new ArrayList<Student>(); As opposed to the old syntax: List studentList = new ArrayList(); Now, this line of code will be the same for either version: studentList.add(new Student("Derek", 1234)); However, one advantage of the generics version is that if we try to add an incorrect value to the studentList.add(new String("Test")); it will be allowed for the old syntax, but will give a compile-time error with the generics version. In addition the behaviour is different when we wish to operate on the data that we take off the list. With the old syntax version we would have to write: List studentList = new ArrayList(); studentList.add(new Student("Derek", 1234")); Student s = (Student) studentList.get(0); System.out.println(s.getName()); Whereas, with the generics version we would have: List<Student> studentList = new ArrayList<Student>(); studentList.add(new Student("Derek", 1234)); Student s = studentList.get(0); System.out.println(s.getName()); So, we no longer have to cast the object of the With the use of generics also comes an enhanced for loop. For example, if we wished to iterate through a
This results in the output: The Usual Way: Derek, Tom, Jane, Jill, Using an Enumeration: Derek, Tom, Jane, Jill, Using the enhanced For: Derek, Tom, Jane, Jill, which is exactly the same for all three cases. These notes are copyright Dr. Derek Molloy, School of Electronic Engineering, Dublin City University, Ireland 2013-present. Please contact him directly before reproducing any of the content in any way. |