Arrays

Lesson 12
Author : Afrixi
Last Updated : October, 2017


Java - Programming Language
This course covers the basics of programming in Java. Work your way through the videos/articles and I'll teach you everything you need to know to start your programming journey!

Up until this point in the course, we've been working with different types of data, and generally we've been using variables to store that data. Variables are great because we can take pieces of data that would otherwise be difficult to remember and keep track of and store them in a nice, named container.

But variables as we've seen them so far do have one short coming, and that's when it comes to storing large amounts of related data.

Many of the programs that you'll find the real world, and that we'll be writing in this course will deal with large amounts of data. It could be a list of names, an index of phone numbers, the answers to a test, or any number of collections of data like these.

When you have lots of data like this that's meant to be grouped together, you can't use normal variables to store it. If you did you would need to create hundreds or thousands of variables, and that's assuming that you even know how many pieces of data would be in the collection.

Luckily for us the problem of storing large amounts of data like this has been solved with something called an array.

An array is a container which acts a lot like the variables we've seen so far in this course, except they allow us to store more than one piece of data.

So if you wanted to keep track of a list of names or phone numbers in Java, instead of storing them in separate variables you could store them in one massive array!

Creating an array

Let's take a look at how to create an array in Java:

Copyint [] luckyNumbers = {4, 8, 15, 16, 23, 42};
//         indexes:    0  1  2   3   4   5

You'll notice that creating an array is very similar to creating a variable. In this case we're storing a list of numbers inside the array. Notice how they're all separated with commas. In this case we're populating the array with numbers right off the bat, but a lot of times in programs you'll start with an empty or half empty array and fill it up as the program executes.

Array indexes

You'll also notice that I've indicated the indexes of each element in the array. Just like strings, which assign indexes to each of their characters, arrays elements also have indexes.

We start with the first element at index position 0, the indexes then increment by one from there. Just like strings you have to get used to the first element being at index position 0. This is something that commonly trips up new programmers!

The array is an entity in itself, but if you want to access an element inside the array you can do so by referring to it directly by it's index. Let's take a look at accessing an element in an array:

CopyluckyNumbers[0] = 90;
System.out.println(luckyNumbers[0]);
System.out.println(luckyNumbers[1]);
System.out.println(luckyNumbers.length);

The elements inside the array are just normal strings, numbers or booleans. So once you've accessed one they can be treated like you would normally treat one of those data types. You can also modify the elements in an array by simply assigning them a new value as you can see above.

Wrapping it up

So there you have it, an introduction to arrays! Take a look at the video above for more information and to get a full step by step walk through of working with arrays!