Skip to main content

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 >>

_______________________________________

Sorting array values

Sorting array values

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;

programming-in-java-a-step-by-step-tutorial-for-beginners-lesson-15

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.

programming-in-java-a-step-by-step-tutorial-for-beginners-lesson-15

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.

programming-in-java-a-step-by-step-tutorial-for-beginners-lesson-15

Exercise:

  1. Modify the testingArrays program so that it will return the average of all the values in the array.
  2. 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.

Scroll to Continue


<< Lesson 14 | Lesson 16 >>

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.

Related Articles