We've talked about the concept of Java Arrays back in “Five basic concepts of any programming language #3 – Data Structures”, so if you like to you brush up on what we talked about, feel free.

What are Java Arrays?

As I've mentioned before, you can think of an Array like a bunch of variables all smashed together under the same variable's name. So, as an example, you have a bunch of contacts in an address book, and instead of creating variables to represent each contact like so:

String contact1, contact2, contact3, contact4 //etc...

You can combine them all into one variable's name like so:

List contacts = new ArrayList();

Now, this code is for an ArrayList, which for me, is used heavily in my day to day programming endeavours. But this is in fact just a flavour of Java Arrays as it's based on a Java List. So allow me to introduce you to the original Java Array.

String[] contacts = new String[10];

Okay, so, two things you'll notice:
1) The square brackets [] 2) We've put the number 10 in there

The square brackets [] are Java's notation for an Array. They just signify that the variable type (in our example, String) will not be just a single variable, but an Array of those variables. So this means that you could throw those square brackets next to any variable type in Java to make them an Array (I'm trying to think of a variable type in Java where that wouldn't work, but I'm drawing a blank, so if there are any programmers out there reading this that know an example where this statement is false, please leave a comment).

Now let's talk about why I put the number 10 in that code. Well, one difference between an ArrayList (think no square brackets needed) and an Array (think square brackets needed), is that with an Array, you'll need to define the size of the Array when it's being initialized. What do I mean by that? Here's an example of code that would fail:

String[] contacts;
System.out.println(contacts[1]);  // this will give you an error

The reason why this fails is because you haven't initialized the Array. So let's re-write this code with the initialization part included:

String[] contacts;
contacts = new String[10];


This will now work, but, you won't see anything displayed in your console, because although you've initialized the Array, you haven't yet actually populated it with anything. So how about we populate it with stuff? I want to keep this example simple, so I'm going to use a loop to populate the Array:

String[] contacts = new String[10];

for (int i=0; i < contacts.length; i++)
  contacts[i] = "person" + i;

So what we've done here is initialized the Array with a size of 10 and we've created a "for loop" that will iterate over each element in the Array and populate it with something. In our case, we're iterating over the "for loop" 10 times, as the "for loop" is based on the size of the Array (which is 10 elements in length). And for each iteration, we are storing the String "person" followed by the index of the current iteration of the "for loop". Sound complicated, but here's the output:


The word "person" is followed by the current index of the Array, 10 times. So wait a sec, why did the loop start with person0 and not person1? You were expecting to see the numbers 1 through 10 right? Well, that happened for two reasons. The main reason it starts at zero is because my "for loop" was coded to start at 0 and run 10 times (i.e. 0 -> 9), but I did that for a reason. The reason I did it is because Java uses a zero based numbering system for its Arrays. So when you want to refer to the FIRST element in an array, you use the number 0. The second element would be index 1, the third would be index 2, and so on. Had I programmed my "for loop" to start from index 1 and go until index 10, we would have seen an error when it tried to output the last element in the Array, because element at index 10 doesn't exist! Only elements 0 -> 9 exist. Make sense? Hopefully it does, if it doesn't just leave a comment below and I'll try to re-word it :)

One neat thing I'd like to point out about Arrays, is that you can have something called a multi-dimensional Array. Now, having seen these in real world coding scenarios, they are a pain in the butt to understand and debug, so I wouldn't recommend using them unless you put plenty of comments in your code describing its purpose. But for the sake of completion, I'll show you how to use them. Let's say you wanted to create a crossword puzzle program.



Here we have a 4x4 multi-dimensional Array. The code to create this crossword puzzle would look like:

String[][] crossword = new String[4][4];

crossword[0][0] = "A"; crossword[0][1] = "D"; crossword[0][2] = "L"; crossword[0][3] = "E";
crossword[1][0] = "Q"; crossword[1][1] = "N"; crossword[1][2] = "N"; crossword[1][3] = "O";
crossword[2][0] = "W"; crossword[2][1] = "I"; crossword[2][2] = "N"; crossword[2][3] = "D";
crossword[3][0] = "V"; crossword[3][1] = "K"; crossword[3][2] = "N"; crossword[3][3] = "E";

Now, having explained what an Array is in Java, I'd like to go back to what I mentioned before about the ArrayList. I find that the ArrayList is more useful in most coding situations, it has built in methods that allow you to do things like adding and removing elements without having to worry about the size of the Array (as it will grow and shrink as necessary). But, it is however, important to know what an Array is in Java and what the syntax looks like.


Free Java Roadmap

Discover exactly what you need to learn and where to start in order to become a professional coder.