ArrayLists

Contents

keyboard_tab

The course is part of this learning path

ArrayLists
Overview
Difficulty
Intermediate
Duration
57m
Students
97
Ratings
5/5
starstarstarstarstar
Description

This course covers the fundamentals of data structures specifically looking at the regular built-in arrays that Java provides as part of the language, as well as the ArrayList class, which lives in the java.util package.

Intended Audience

  • Beginner coders or anyone new to Java
  • Experienced Java programmers who want to maintain their Java knowledge
  • Developers looking to upskill for a project or career change
  • College students and anyone else studying Java

Prerequisites

This is a beginner-level course and can be taken by anyone with an interest in learning about Java.

Transcript

In the last lecture, we talked about the regular built-in arrays that come with the Java programming language. They are some of the simplest data structures or containers that we can utilize in Java. They have some limitations, such as not being able to grow in size as many items are added. They are fixed in size. In this lecture, we will discuss the ArrayLists class which lives in the java.util package. Unlike regular arrays, the ArrayLists can grow as needed to accommodate elements as we need to. The syntax is a little different from what we've encountered before but is pretty straightforward. So, let's dive in. First, let's create an ArrayListFun file, right click 'New', 'Java Class', ArrayListFun. 

Okay, so we create the ArrayListFun class and now we will create public static void main. As a little side note, I think you can now figure out what this parameter to the main method is, at least what type it is. That's what the brackets are for because it's an array. It's a built-in array. We need the java.util.ArrayList; to be imported so we can use it. So, let's create the ArrayList<String> of type String, namesList = new ArrayList(); and it used to be you had to put the data type in <> braces here as well. We're going to delete that because it also knows it based on this. So, we don't really need it twice. It's redundant. So, this is how you're going to add elements to the ArrayList, namesList.add("John"); namesList.add("Kyle"); namesList.add("Matthew");  namesList.add("Amanda"); and namesList.add("Wendy"); And let's loop through them here for(int i = 0; i < namesList.size(); i++) And we're going say System.out.println(namesList. And now in this case instead of add, we're going to say get(i)); Notice these are parenthesis, they're not [] brackets like we used with arrays. So, we use the add() method, and the get() method, and the size() method. So, on regular arrays we have the length property. 

Now we have the size() method. Now let's run it. John Kyle, Matthew, Amanda, and Wendy. So, the same order that we added it on. Great. So, the names print out as expected. One difference to note with ArrayLists versus arrays isn't the declaration. So, again you use these <> braces, these like < and > symbols and the arithmetic context surrounded the data type in this context. We used the new keyword just like with regular arrays right here, but you need the <> braces again with the constructor for the ArrayList on this right side over here and again you could put a cynical in there and then split it up if you wanted to into two lines that would work as well, just like this. 

So, you'll notice we don't specify a size that we don't have to. ArrayList as mentioned, can change their own size as needed to accommodate as many or as few elements as we wish to store them. Notice our use of the methods again, add() when we want to add the names to the ArrayList and the get() to retrieve the values. And of course, again we use the size() method instead of the length field like we were using with regular arrays. Another not so obvious difference, at least from this example, is if we try to use a primitive type with ArrayLists. So, let's try declaring an ArrayList of integers below our namesList. So, we put it right here, ArrayList and we'll try it with int, myNumList = new ArrayList<>(); And let's see what happens. So, it's underlined in red. 

So, notice this error that we get says, type argument cannot be of primitive type. Well truth is, ArrayLists only hold reference types, not primitives. Therefore, they store the addresses of all the data they want to maintain. This seems like a significant limitation, but we'll see that there is a good way to overcome this limitation in the next lecture. Before we move on however, I want to issue you a simple challenge. In the same ArrayListFun file that we've been working in, I want you to comment out the current for loop and try using an enhanced for loop syntax that we learned earlier for printing out the names. So, pause the video, come back when you're done or if you need some help. How that go for you?

Hopefully, that wasn't too difficult and you can pull all your knowledge that you've gained in the previous lectures and maybe figure out what we're supposed to do. But let's change our usage of the regular for loop to the enhanced for loop. Well, first of all, I'm going to get rid of this because there's an error there, and we're not using it anyway. And let's comment this guy out right here, and we're going to use the enhanced for loop. So, for(String name : nameList)  and then we're going to System.out.println(name); each name individually. Let's right click and run it. 

And we get what we expect; John, Kyle, Matthew, Amanda, and Wendy again. So, works perfectly. Awesome work, everyone. ArrayLists have tons of other awesome methods to help us write powerful code, but we got a solid taste of them here. In the next lecture, we will discuss a special set of classes that provide us with a lot of cool power and also help us to overcome the reference type limitation that we were discussing with our ArrayList. I'll see you there.

 

About the Author
Students
763
Courses
20
Learning Paths
4

John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.

Covered Topics