Skip to main content

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.

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

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.

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

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.

Scroll to Continue

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++;
         }
     }
}
programming-in-java-a-step-by-step-tutorial-for-beginners-lesson-14

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

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

Related Articles