Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 15
Danson Wachira is a certified Trainer in Computer Science, Information Technology and related studies.
Table of Contents
<< Lesson 14 | Lesson 16 >>
_______________________________________
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:
import java.util.Arrays;
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.
Exercise:
- 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.
<< Lesson 14 | Lesson 16 >>
Related hubs...
- 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.
Want to make money online?
Comments
Danson Wachira (author) from Nairobi, Kenya on March 11, 2013:
Hi Jakov Andric,
It should, plus myArreys[i] % 2 (greater than) 0 will also work great for myArreys[i] % 2 != 0 as the expression is a bit faster on the compiler. Thanks for the visit and comment.
Jakov from Varazdin on March 11, 2013:
Oh my bad sorry, mixed odd and even. Thx for replay dwachira.
Does this work?
for (int i = 0; i (less than) myArreys.length; i++){
if(myArreys[i] % 2 != 0 && myArreys[i] != 0){
System.out.println("Odd value in array: "+ myArreys[i]);
}
For values:
myArreys[0] = 0;
myArreys[1] = 5;
myArreys[2] = 10;
myArreys[3] = 7;
myArreys[4] = 16;
...output is:
Odd value in array: 5
Odd value in array: 7
Danson Wachira (author) from Nairobi, Kenya on March 10, 2013:
Hi Jakov Andri,
Good attempt but in the exercise #2 we needed the odd values. The reminder of an odd value divided by 2 is always a value greater than zero and not a zero. Thanks for the comment and participating.
Danson Wachira (author) from Nairobi, Kenya on March 10, 2013:
Hi ah seng,
This tutorial is designed for beginners and the best way to take it is to follow lesson by lesson as one lesson can be as a result of another. Thanks for the visit and comment.
Jakov from Varazdin on March 10, 2013:
Exercise solution:
1)
int myArreys[];
polje = new int[50];
myArreys[0] = 11;
myArreys[1] = 12;
myArreys[2] = 10;
myArreys[3] = 7;
myArreys[4] = 16;
Arrays.sort(myArreys);
float avrage = 0;
for (int i = 1; i (less than) myArreys.length; i++){
avrage = (avrage + myArreys[i]);
}
System.out.println("avrage: " + avrage/myArreys.length);
2)
int polje[];
polje = new int[50];
myArreys[0] = 11;
myArreys[1] = 12;
myArreys[2] = 10;
myArreys[3] = 7;
myArreys[4] = 16;
Arrays.sort(myArreys);
for (int i = 1; i (less than) myArreys.length; i++){
if(myArreys[i] % 2 == 0){
System.out.println("Odd value in array: "+ myArreys[i]);
}
}
ah seng on March 09, 2013:
good! but the second tutorial,, the collection is intermediate level aren't they?
Danson Wachira (author) from Nairobi, Kenya on August 30, 2012:
Hello teaches12345, thanks for the visit and comment, i really appreciate the support here.
Dianna Mendez on August 24, 2012:
Good tutorial on this subject. I am sure it will be helpful to those needing the information you posted here.