- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 13
Lesson 13: Implementing Arrays in Java
Welcome to Lesson 13 of Programming in Java. The concept of arrays and using arrays in programming is vital to every programmer.
You need to know how to use arrays efficiently in programming and how to manipulate arrays in Java programming language.
In the previous lessons, we have been working with variables that can hold one value only. It would be better if we are capable of storing multiple values in a single variable. To do this, we have to introduce arrays into programs.
Definition of an array
An array is a list store of values of the same type. If we have 5 values of ages from five students, then the values of these ages can be represented as shown:
Example of array
Array index
| Age values
|
---|---|
0
| 17
|
1
| 14
|
2
| 16
|
3
| 14
|
4
| 15
|
In Java, the index or the position of the elements (values) in the array start at 0 (zero) so, the first value will be at position zero moving to 1, 2, 3 and so on.
To define an array of numbers as shown above, we have to specify what type of data will be stored in the array (integers, strings, boolean values, etc).
We also need to specify the size of the array i.e. how many positions will be there in the array. To do this, we use the following statement:
int[ ] myNums;
The above statement tells Java that we want to store number values (int) in the array, the name of the array is myNums.
The two square bracket alerts Java that our variable is an array variable but for us to specify the size of the array we have to include another statement.
myNums = new int[5];
Here, we have created a new object of the array type and specified that we need to have five positions as the size of the array. The two statements can also be joined as shown:
int[ ] myNums = new int[5];
If we execute the above statement now, Java will initialize all the five positions with zeros as it wait for the actual values to be assigned to these positions.
Assigning values into an array in Java can be done dynamically or statically.
To assign a value of 17 at the first position in the array, we write the following code:
myNums[0] = 17;
We have specified position 0 inside the square brackets because array index in Java start as zero, so the first position will be at index zero.
And to assign a value of 14 to the second position in the array, we write the following code:
myNums[1] = 14;
We can also assign all the values into the array statically as shown:
int[ ] myNums = {17,14,16,14,15};
And for values of string type the assignment will look as shown below:
String[ ] Workingdays = {"Monday", "Tuesday", "Wednesday", "Thursday" , "Friday"};
You have to be careful when setting values for a Boolean variable using the above method as we cannot do this: boolean[ ] myBools = {false, true, false, true};
To assign values of Boolean type using the above method, we need the new keyword as shown below:
boolean[ ] myBools = new boolean[ ] {false, true, false, true};
To display a values from a given array position, we can use the following statement:
System.out.println( myNums[1] );
The above statement will pick whatever value that is in the array index position 1 and display it on the console.
Let us write a program to output values from an array. Go to the NetBeans code window, create a new Java class, call it testingArrays. Type the code as shown and run.
Example of array in Java
Assigning values into an array one by one as shown above can be tedious when you are working with many values. In the next lesson, we shall look at how we can manipulate Java arrays using various loops to simplify allocation of many values into an array.
<< Lesson 12 | Lesson 14 >>
Related hubs...
- 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 3
In Lesson 2, we saw how the Java code window looks like and we were able to interpret the various parts of the Java code. In this lesson we’ll learn how to write our first running Java program. - 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. - Programming In Java - A Step By Step Tutorial For Beginners: Lesson 1
Java is increasingly becoming the world’s programming language of choice. It has been used in developing desktop applications, client-server applications and even mobile phone applications including games.