Tag Archives: array

How to sort (and reverse sort) an array of strings

This program demonstrates how to use Arrays.sort(…) to sort an array. There is also a more advanced example that uses a comparator to sort the array into reverse order. I’ve included a utility method dump(…) to output the array contents … Continue reading

Posted in Uncategorized | Tagged , , | Leave a comment

Working with arrays of objects

Consider the following UML class diagram which shows the relationship between a superclass (Vehicle) and two subclasses (Motorbike and Car). Because Motorbike is a Vehicle and Car is a Vehicle, we can create an array of vehicles like this: The … Continue reading

Posted in Uncategorized | Tagged , , , , | Leave a comment

Array .length property and String .length() method

You probably already know that we can use the length() method to get the number of characters of any String object in Java. For example, Knowing the length of the string is really useful as it allows us to calculate … Continue reading

Posted in Uncategorized | Tagged , , , , , | Leave a comment

Java program to summarise list of student exam grades

Java programming exercise: You have been given a list of student exam grades (A – E). Choose a suitable data structure to represent the list and write a program to calculate totals for each grade letter. My solution:

Posted in Uncategorized | Tagged , , | Leave a comment

How to use arrays in Java

Arrays implemented in Java are of a fixed length and hold values of a single type (e.g. String). Each item in an array is called an element, and the first element is indexed at zero. The following code demonstrates three … Continue reading

Posted in Uncategorized | Tagged , , | Leave a comment