Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 14
Danson Wachira is a certified Trainer in Computer Science, Information Technology and related studies.
Table of Contents
<< Lesson 13 | Lesson 15 >>
Lesson 14: Manipulating Java arrays using loops
In lesson 13, we learnt how we can define an array in Java programming language and how we can assign values into the array. To assign values, we used the following statement:
myNums[2] = 14;
The above statement of assigning values into an array will work best only if we have fewer values to store in the array, but what if we have many values, say over a 100 number of values? It will be tedious to use such a statement of assigning values one value by one. To simplify our programming, we introduce the loops that we learnt in the previous lessons; FOR, DO…WHILE and WHILE loops.
FOR loop and Arrays in Java
If you want to store consecutive numbers in the array, you need to know how many positions are there in the array.
You don’t want to assign a value to non-existing position in the array. To establish the number of position in the array, we use a property of the array called Length.
We’ll write another program to store multiples of 10 from 1-50 into an array. Go to the NetBeans code window, create a new Java class, call it loopsArrays. Type the code as shown below and run.
Here, we have defined our array to have 50 positions, we have then looped through the array and for every loop we multiply the value of counter i by 10 and store the value into the array. The last statement inside the FOR loop is for outputting the values of the array on console.
WHILE loop and Arrays in Java
Let us try the same example with a WHILE loop. Copy and Paste the code as shown below and run.
Java program using WHILE loop and Arrays
package myfirstprogram;
public class loopsArray {
public static void main(String[] args) {
int[] myNums;
myNums = new int[50]; //Define an array of size 50
System.out.println("Array values are:");
int i=1;
while(i<myNums.length){
myNums[i]=i*10; //Multiply i by 10 to get multiples of 10
System.out.print(myNums[i]+"," ); //Output array values
i++;
}
}
}
DO…WHILE loop and Arrays in Java
Exercise:
Modify the above example to work with the DO…WHILE loop. If you have problem with the loop, re-visit Lesson 12 where we learnt about DO…WHILE loop. In the next lesson, we shall look at how we can sort values in an array, see you then.
<< Lesson 13 | Lesson 15 >>
Related hubs...
- 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. - Programming In Java - A Step By Step Tutorial For Beginners: Lesson 9
In this lesson we shall learn how to use the Switch statement in Java programming. Switch statement is a selection statement, that means when used, it select one value among many values. Switch statement can also be used instead of IF .. ELSE ... - Programming In Java - A Step By Step Tutorial For Beginners: Lesson 6
In this lesson we’ll learn about another useful class for accepting user inputs and displaying results. This class is called JOptionPane class and is located in the javax.swing library. - Programming In Java - A Step By Step Tutorial For Beginners: Lesson 5
One of the useful classes that handle inputs from a user in Java is the Scanner class. The Scanner class is located in the util (utility) package of the Java library.
Want to make money online?
Comments
Erick on January 25, 2013:
This is a nice tutorial with very easy to follow instructions, thanks
Danson Wachira (author) from Nairobi, Kenya on August 21, 2012:
I like the suggestions here Lord De Cross, i will surely do those reviews. Thanks for the visit and comment. Enjoy your day!
Joseph De Cross from New York on August 21, 2012:
I took some C++, So I can see where is heading this programming into. You probably know about pythom and SQL. Great review Dwachira! Now where we will see html6.0 and C+++?