Lesson 19: Arrays

So far, we have gone over several different types of data we encounter in Java.  Some of these data types include: chars, Strings, ints, and doubles.  However, as our programs become more complex and start to process more info, we will find that having large quantities of variables of the same type can become a tedious task.  Luckily, Java has a way to organize and store our data through the use of Arrays.

Arrays are composed of a singular variable type and have index points to access the different variables.  Also, Arrays can only have one variable type.  For example, you cannot store an int and a String in the same array.   

There are several different ways to declare an array.  The two following lines of code both declare an array, but have the brackets in different places. 

We we are talking about an array’s length, we are referring to how many different spots we have in our array where we can store data.

Next, if we want to assign our array’s length, we can use either way implemented below:

In his example, we can see that the length of the array is 4. This is because it has an index of 0, 1, 2, and 3. That means that we can put 4 separate Strings into our array.

It is also important to note that the size of an Array cannot be changed once it is declared.  In the example above, our names[] array would only be able to have a maximum of 4 Strings. We are unable to increase or decrease the size of the array.

Although the code above does create an array, there is still no data in it.  So let’s now make an array with and put our data in it. Since we are creating our array and putting our Strings in it simultaneously, we do not have to put in an index number because Java will automatically assign it three index points. Let’s make an array with three names in it:

In order to fully understand what the code above means, lets break it down.

String is the type of data we want to store in our array

names is the user assigned name to our array

[] The brackets tells the computer that we want to make an array 

= is the assignment operator

{} The curly brackets are used to encapsulate all of our various Strings 

Then, inside of the curly brackets, we have our 3 different Strings declared just as we would with a normal String – within quotation marks.  Finally to separate our individual Strings, we use a comma between each String.

Now how do we access the data within our array?  All we have to put in the name of our array and put it the index point you want accessed.

For example, if we tell Java to: System.out.println(names[0]); we will get an output of: Sam.  This is because we are asking Java to print out the names variables at index 0.  Just remember when working with arrays that the first index point will always be 0, and not 1.

In this lesson, we have only just scratched the surface of arrays. We will learn more about them in future lessons and how useful they are in Java.

GitHub link.

Leave a Reply

%d bloggers like this: