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

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

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.

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

## Want to make money online?

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