Coding Jar

Hello Woofers!

I am here, once again, to teach you woof to iterate an Array in Java! I'm serious, I didn't know, until today, Woof to iterate this thing!

Well, now I know, and the ingredients for this recipe are simple:

- create an array with a name ar (eg ar from array) which will have 8 array elements (eg 1,2,3,4,5,6,7,8)


- two extra integers, one for my loop counter and one for the assignment of each element of our array.

My code is below:

// Java program to iterate over an array 
public static void main(String args[]) {
  int ar[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; 
  int i, x; 

  // iterating over an array 
  for (i = 0; i < ar.length; i++) { 

    // accessing each element of array 
    x = ar[i]; 
    System.out.print(x + ", "); 


* the +", " connects a String to our integer. With this addition, you will have spaces and commas and anything other info you want between your array elements.
** If you want each element to be printed in another line, you can simply use the System.out.println(); 🙂

When you compile the code (I use Netbeans in my iWoof Pro), the result will be this:

1, 2, 3, 4, 5, 6, 7, 8

Well Done!
You just created and iterated an array!

Arrays are very important for both simple and complicated projects, and especially for game development!

You can find out more from my Hooman Friend in #Quickiversity

This was Hector Codes, from your best doggo fren, Hector!

Share this