COMPSCI4039 Programming
Arrays Part 5
Sorting arrays – a better strategy
Copyright By PowCoder代写 加微信 powcoder
• When you have paired arrays that are linked to one another, you often will want to create a class to hold that information instead.
• Classes are very useful in the sense they allow us to group related information and behaviors together!
• So if we want to create a game that has the following information: • Scores
• It is better for us to create a class to store this information together.
What does the class look like?
• This class will be used instead of the 2 separate arrays.
• The principle is to have an array of objects instead.
public class Profile {
private String name;
private int score;
public Profile(String name, int score) {
this.name = name;
this.score = score;
public int getScore() {
return this.score;
public String toString() {
return this.name+ ” scored: “+this.score;
• This array then is our sole concern when we want to organize the high scores!
A new concern
• When creating objects from a custom class, we need to tell Java how to sort them.
• Previously we had an array of integers. • Relatively easier to work with.
• We simply check values and work with them.
• Need a slightly different approach with objects.
A new concern
• Swapping integers in an array comes down to checking the values of the array offsets.
• But it doesn’t make sense to check if ‘profile1’ is greater than ‘profile2’
• We need to do something else.
• We need to specify to Java how to compare Profile objects!
Is profile 1 > profile 2?
Doesn’t make sense to do this
How do we sort objects?
• Objects allow us to encapsulate multiple concerns.
• But that comes with additional complexity when coding the class.
• However, this will save time when we implement logic that utilizes that class! • When sorting an array of objects, we often need to specify a given
metric, concerning how we will sort them.
• This metric can be anything.
• What it will be is largely dependent on your usage context.
• Let’s see how we can improve on our sorting problem!
Which is better?
• Realistically, there is nothing stopping you form using dual arrays if you want to.
• Whether you prefer to use dual arrays, or a class is personal preference.
• There are however some distinct advantages to using objects/classes.
1. Easier to read code.
2. Encapsulation 3. Extendibility.
• Extendibility is significant, if we make changes such as add another array to represent the number of lives a player has. Then this means we will have to update the sorting algorithm in turn if we have paired arrays!
Here is the best way… get Java to do it!
• Up until now, our sorting algorithm has been done manually. • i.e., we have specified code that tells Java how to sort the array.
• Java has a built-in static method called Arrays.sort()
• You pass this method an array, and Java will sort the array for you!
• Java knows how to sort arrays of primitives natively, and it knows how to sort objects from its native classes such as Scanners.
• But Java doesn’t know ahead of time how we want to compare objects from classes we write ourselves.
• Java must know how to sort the array.
• Specifically, it must know how to compare objects in the array.
How do we tell Java how to compare our custom objects?
• Interfaces.
• Classes that implement interfaces must implement the methods of an
interface.
• It is essentially used to enforce a coding standard when writing classes that will talk to, or ‘interface’ with other objects.
• Comparable is an interface that enforces the use of a single method compareTo
• If a class implements this interface, then we can use Arrays.sort()
What happens under the hood?
• Java uses an int value to determine, given two objects their order of precedence.
• How java arrives at that int value, we code inside the compareTo method.
• If given some logic, the value returned is 1, the the current object should come after the one being compared with. If -1, it should be before, if 0 then it doesn’t matter.
• Let’s see all this coming together in code!
The key differences
• The comparable interface means that we are essentially programming a series of instructions to Java on how it should compare a pair of objects from a class.
• This means, when we call Arrays.sort() and pass it an array, it will be able to check each object pairing in that array and understand how to sort them.
• It gets the sort instructions from the class itself.
• Therefore, we only need one piece of code to sort the array!
• Previously, we were simply telling Java in code how to sort the objects.
• This means we would have to write custom rules every time we want to sort different objects.
Finishing up
• You will learn more about interfaces in the ADS course.
• Try and avoid pairing arrays if you can, it is often easier to use classes.
• If you define your own objects, you need to implement comparable to tell Java how it should sort your objects.
• Arrays is a Java class that contains several different static methods
for handling arrays:
○ sort()//alreadyseenthis
○ binarySearch()//lookforaparticularvalueinthe
○ copyOf()//copypartofanarray
○ fill()//fillthearraywithsomevalue
○ And more…
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com