Java basics Part 4 – Array

In this article I am going to tell you about, how to create your own array with Java. Arrays are used to store information and making calculations based on them.

Theory time

So why to have arrays? Well arrays are basically used to store same type of data to array shaped style. This means that coder is able to take user feed and store it. When enough data is collected program is able to make calculations and other conclusion based on given feedback.

In Java arrays first place number is 0. This meas that if your arrays length is 5, last place number is 4. Still 5 different data can be stored in the array. Maybe this picture can tell you more.

How the information is stored when using Java, the count here is 5

How the information is stored when using Java, the count here is 5

Getting started with arrays

To use arrays you have to initialize them like everything else you are trying to use. This is one way to initialize them

 final int MAX = 5;
 int [] array = new int [MAX];
 int count = 0;

So what does that mean? Well to go for line from line the code first you can tell some constant or variable data. Here we have final int MAX. Then from the sexond line, first you initialize the array givening which kind of data type array you are creating. For this example it is int -type array. Then you give amount that indicates how big is the array (MAX here means 5). For the last line we have variable count that has been initialized. This specific variable is the one of the key factors, because this variable indicates the place number where the data goes.

The you might ask how can I store user feed to array. Well here I recomend using for-repetation sentence. By using code above you still need to use scanner input and create new variable. This code is placed on main. Remember also import the scanner: import java.util.Scanner .

 Scanner input = new Scanner(System.in);
 final int MAX = 5;
 int [] array = new int [MAX];
 int count = 0;
 int number;
 for (int i = 0; i < array.length; i++) {
 System.out.print("Give the " + i + ". number: ");
 number = input.nextInt();
 input.nextLine();

 array[count] = number;
 count++;
 }

 for (int i = 0; i < array.length; i++) {
 System.out.println(array[i]);
 }

 input.close();

So as you can see even this is basic thing, it’s still quite much typing.

You may fonder what does that array.length mean, it means the length of the array. In this example the length is 5. Why to use that sentence instead of just typing the number 5 in the for-sentence is because when using array.length you always get the length assuming you have done everything right. You can use other statements of course there.

Last but not the least, here is an example program that uses arrays. What does it do, it first ask, how many players takes part of some game. Then it ask results of each competitor and shows the results. For the last it prints the highest point score and who player got it wins.However the program doesn’t tell which player did have that score .

package basic;

import java.util.Arrays;
import java.util.Scanner;

public class ArrayExample {

 public static void main(String[] args) {

 Scanner input = new Scanner(System.in);

 int players, score;

 System.out.print("Give the amount of players: ");
 players = input.nextInt();
 input.nextLine();

 int [] totalscore = new int [players];
 int count = 0;

 for (int i = 1; i <= totalscore.length; i++) {
 System.out.print("Give the " + i + ". players score: ");
 score = input.nextInt();
 input.nextLine();

 totalscore[count] = score;
 count++;
 }

 System.out.println(totalscore.length);

 for (int i = 0; i < totalscore.length; i++) {
 System.out.println(totalscore[i]);
 }

 System.out.println("Results are following: ");
 for (int i = 0; i < totalscore.length; i++) {
 System.out.println ( i+1 + " Player score: " + totalscore[i]);
 }

 Arrays.sort(totalscore, 0, count);

 System.out.println("Player with the score " + totalscore[count-1] + " won!");

 input.close();
 }

}

Conclusion

Using arrays may help you to categorize results or even help you to get results you want. I recommend to try to use arrays because it is really good way to richer your code.

– Tuomas Törmä

Sources

  •  Studies in Haaga-Helia Autumn 2013 course Introduction to Programing Teacher Lahtinen Seija
  • Try and err

Leave a comment