Home > Software engineering >  Displaying an Array using for loops
Displaying an Array using for loops

Time:11-16

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  )

CodePudding user response:

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]);
}

CodePudding user response:

You can access each element using myArray[index], for example myArray[0] is 8 and myArray[2] is 5. In your loop i will be your index going from index 0 to index myArray.length.

  • Related