Without Java arrays, you're doomed to a life of creating variable after variable, slaving away for countless hours, and watching your code get larger and larger.
By now you should know how to create variables. If not, go back to the Java for Beginners tutorials and read up on how to create and use variables. You will not understand any part of this tutorial without that prior knowledge. If you do, great! It's time to learn how to create lots of variables very quickly.
A Java array is created using the following format:
In the first part of that code, you saw int. This is how you create an array of integers. It looks almost like creating a regular int variable, but notice how there are square brackets next to it. That means that we want to create an int array, and not a single variable.
Numbers is what we're calling this integer array. That part is the same as a regular variable.
The part after the equals sign is sort of new, but it's actually something you've seen before. Do you remember this:
If you do not remember this statement, then I recommend reading the Java input tutorial. Even if you know other ways of getting input in Java, it's useful to know what a Scanner does in case you ever see it again.
We are creating a new array the same way a new Scanner is created. Arrays are not created from classes, but they are a little more advanced than the basic types.
You'll note that there's a number inside the other set of square brackets. This number is the size of the array. In other words, it's the number of integer variables you want to have. I chose 5 for the example, meaning I want to create 5 integer variables.
We have an array, now what? You will probably want to know how to have all these variables stored in just this one array variable. See, an array is just a bunch of storage spaces. Each of these spaces is given a number, starting with the number 0. Here's a diagram to help illustrate what I mean.
To access one of these storage squares, you simply give the number of the space you want after the variable name. Let's look at our previous example and access the first element (the storage space marked as a 0).
That stores the number 5 in our integer array called numbers, and it stores it in space 0 which is the first spot in the array.
Ok so now we can do this:
Notice how I stopped at 4 and not 5. What would happen if I did this:
I would get an ArrayOutOfBoundsException. Basically, Java will telling me that I have gone too far, and that there is no space 5. Remember we had said that this array only has 5 spots, and those 5 spots are 0, 1, 2, 3, and 4. There is no 5, and Java will complain and your program will not work correctly. You won't actually get an error, this will happen once your program has already started, so you have to be extra careful!
But wait a minute. What if you need 200 variable spots? This will still take a really long time to code!
If the data you're storing is totally random, this might be true, but if you want to store say, the numbers 1 through 200, there's an easy way to do this. Remember for loops?
First thing you'll notice is that I did numbers.length. This is built into Java to give me the size of the array. Since I declared the numbers array to have 5 storage spots, numbers.length in this case is the same as writing in 5. So, my for loop will loop 5 times.
The next thing I did is put the variable x inside the square brackets. Remember x is just whatever number the loop is on, so it will continue to go to the next storage space every time it loops. Oh, I'm also making the variable store x, so it will also store whatever x is during that loop.
Basically, I'm storing the number 0 in storage space 0, the number 1 in storage space 1, all the way up to storage space 4 (there is no 5 remember).
You can actually create Java arrays of unknown lengths immediately, unlike C++ arrays and arrays from a lot of other programming languages.
Above I'm getting user input to tell me how big to make the array.
You can also preset the values of the variables inside the array. Here's an example of creating a char array, but creating it with the values already inside.
This creates a character array of length 4, because there are 4 elements inside the comma separated list given inside curly brackets. You can put as many or as few elements as you like inside the curly brackets, just make sure those elements are separated by commas. The array is automatically the size of the number of elements you provide.
In case you forgot, the reason those letters have single quotes around them is because char values always have the single quotes around them.
Note: Once the size of an array variable is given, it cannot easily be changed, so if you need an array to hold a large number of values, try to figure out the absolute maximum size the array might have to be.
In the next tutorial we're gonna have another look at Java methods. As always, I'll take any questions or comments in the form below. Until next time...
If you have any questions, comments, or concerns, feel free to contact us.