Displaying an Array using for loops

I have an array with some set numbers and am looking to display the index and value of each using a for loop.

Here is the array:

int[] myArray = {8,4,5,21,7,9,18,2,100};

And here is my loop so far, just don’t know what to put inside:

for(int i = 0; i < myArray.length; i++)

>Solution :

You can access the value of index i using myArray[i] in the loop

for(int i = 0; i < myArray.length; i++) {
    System.out.println("index = " + i + ", value = " + myArray[i]);
}

Leave a Reply