Iterating Over an Array in Java and JavaScript

3 min read

\"mac\"

I have learned that while Java and JavaScript are different, they share similarities. Someone in the Code Newbie Slack informed me of the history of JavaScript and how the creators had borrowed some functionality from Java. That’s for another post on another blog.

But while I was reading my textbook chapter on arrays, I noticed that you can iterate through an array in Java much like you can in JavaScript.

Arrays in Java are initialized completely differently as you must specify a data type and the slots you allocate in the array cannot be changed, as such, it is immutable. Here is an example of an array in Java:

int[] myArray;
myArray = new int[10];

This declares the array then initializes it with 10 int spots.

An array in JavaScript looks like this:

var myArray = ["Jerri", 69, "Tiffany", 37, "John", 39];

[1]

How Interating Through Arrays in Java and JavaScript Are Similar

\"they_all_look_the_same\"

So, I first got a taste of iterating through an array in JavaScript while working the Bonfires at Free Code Camp, which use algorithms to help you better understand the ins and outs of the language. One of the ways you can iterate through an array in JavaScript is by a for loop and a .length method depending on what you need. Here is the syntax of an algorithm I wrote in order to find out if a word was a palindrome or not:

function palindrome(str) {
 str = str.toLowerCase().replace(/[W_]/g, "");
 for(var i = 0, len = str.length - 1; i < len/2; i++) {
 if(str[i] !== str[len-i]) {
 return false;
 }
 }
 return true;
}




palindrome("eye");

Breaking It Down

The first part of this algorithm is turning all the letters in the string to lowercase and then using regex to get rid of capital letters, spaces, and punctuation[2].

The next part checks each index of the array to see if it is the same later, going inwards until it reached half the length of the array. If they are the same letter, then it’s a palindrome and will return true.

Same with Java…in a Way

The for loop in a Java array iteration looks the same when you just break it down into the method body. For instance:

public static void showArray(int|) array)
{
 for (int i = 0; i < array.length; i++)
 System.out.print(array(i] + " ");
}

Here, when the showValue method is called, an element of the array is passed to the method. It will loop through and print out each value of the elements of the array. Here we are printing it out. In the JavaScript example, we were cycling through the array to test for similarities.

The parallel? You can cycle through, and get elements/indeces of arrays with a for loop.


  1. You need to have the same data type in a Java array. If you look at the JavaScript array, I have number data types and strings. In the Java example I have 10 ints, which are numbers up to 231–1. Can’t mix data types in Java arrays.  ↩
  2. I used regex because there was a test that had to be run that included numbers and odd punctuation that had to return false.  ↩

CC BY-SA 4.0 Iterating Over an Array in Java and JavaScript by Tiffany White is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.