Arrays in Java Application
Submitted by GeePee on Monday, May 25, 2015 - 23:01.
The following Java program application uses Arrays. The program reads five numbers, find their sum and print the numbers in the reverse order. I will be using the JCreator IDE in developing the program.
To start in this tutorial, first open the JCreator IDE, click new and paste the following code.
Sample run:
prints the numbers in the reverse order.
- import java.util.*;
- {
- {
- int[] items = new int [5];
- int sum;
- int counter;
- sum = 0;
- for (counter = 0; counter < items.length;
- counter++)
- {
- items[counter] = console.nextInt();
- sum = sum + items[counter];
- }
- for (counter = items.length - 1; counter >= 0;
- counter--)
- }
- }
Enter five integers:
5 6 6 2 4
The sum of the numbers = 23
The numbers in reverse order are: 4 2 6 6 5
Why do we need Arrays?
In previous tutorial, you already know how to read numbers, print them and find their sum.
The different here is that we want to print the numbers in reverse order.
We cannot print the first four numbers until we have printed the fifth.
This means that we need to store all the numbers before we can print them in reverse order.
An array is a collection of a fixed number of variables called elements, wherein all the elements are arranged in a list form.
The statement int[] items = new int [5];
declare an array item of five elements.
The statement - for (counter = items.length - 1; counter >= 0;
- counter--)
Add new comment
- 55 views