Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 15
Lesson 15: Sorting values in Java arrays
When we store values in an array, we may need to view, display or print those values in some given order. we can also sort those values before we display or print.
Java programming language has inbuilt methods that can be used to sort values stored in an array.
To use any method in Java programming language, we use the keyword Import followed by the packages name and the method name we need to import.
The sorting inbuilt method is called Arrays and to use it, we need to import it into our program using the following statement:
Now, let us re-visit the testingArrays program we did in Lesson 13. Modify the program and insert the above statement just after the package name and before the class name. Here it is;
To sort the values in the array, we use the following statement just before we display the values:
Arrays.sort( myNums );
Notice that this time we don’t need any square brackets after the array name. For us to be able to output all the values from the array sorted, we’ll use a FOR loop to go through the array as we output the values.
Modify your program so that it will look like shown below. Run the program and you should be able to get the values in the array sorted in ascending order.
Sorting array descending however may look tricky for now as it is not straightforward as sorting in ascending. It may require you to create integer objects and then import them from a collection library. Don’t worry about this, we shall re-visit this lesson once you get more used to Java objects. Here is an example if you would like to try all by yourself.
- Modify the testingArrays program so that it will return the average of all the values in the array.
- Modify the testingArrays program so that it will display only the odd values from the array
Visit the next Lesson where we shall look on how we can implement and manipulate multi-dimensional arrays in Java programming language.
- Programming In Java - A Step By Step Tutorial For Beginners: Lesson 2
In this lesson you will learn how to create your first Java project and how to interpret the NetBeans code editor window.
- Programming In Java - A Step By Step Tutorial For Beginners: Lesson 10
The FOR loop is one of the most common looping controls used in Java. The FOR loop forces the program to repeat a statement or a group of statements a specified number of times. It has three parts; the initial value part, condition part, and ...
- Programming In Java - A Step By Step Tutorial For Beginners: Lesson 4
In Lesson 3, we learnt how to write a Java program in the Java code window and how to Run a Java program. In this lesson, we shall learn how to work with variables in Java and how to manipulate computer memory using variables.
- Programming In Java - A Step By Step Tutorial For Beginners: Lesson 8
IF .. ELSE statement has two parts, one to cater for when condition is TRUE and another for when condition is FALSE.