Join Peggy Fisher for an in-depth discussion in this video Using ArrayLists, part of Learning Java.
- You might have noticed that so far, all the arrays had primitive data types stored in them. Well, there's a reason for that. Arrays can only hold primitive data types. So we need a way to store objects in the same way. This is where an ArrayList can really help. Another benefit of an ArrayList is that you could increase and decrease the size dynamically, it's not a fixed size. The downside if ArrayLists is they take up a little bit more overhead. So an array is a good choice if you do know the size of your array and you're only dealing with primitive data.
Okay, let's talk about a realistic example. Remember our real estate business? It might be nice to keep track of many properties without having to create new variables each time. Instead, we can create an instance of the property class and use an ArrayList to store all of them together under one name. Let's review the syntax for an ArrayList. I have a new project started called, RealEstateMLS. I'm gonna go ahead and add javautil.scanner, just in case.
Import java.util .scanner. Okay, in the main part of my program, this is how we define an ArrayList. We type in the word, just like I said, ArrayList. Now, ArrayList is a capital A and a capital L. It's very important 'cause remember, Java is case sensitive. Now, this is new, we're gonna add an angle bracket. If you've programmed in HTML, you're used to the angle brackets. But in our case, inside the angle brackets we have to specify the type of objects we're gonna put in there and we're gonna put Property objects in there.
So we type in the word Property and we put a close angle bracket. Now, we need to give this a name. So let's go ahead and just call it "mls," for multiple listings. Equals, a new ArrayList. ArrayLists are also classes and objects, so you have to instantiate them. On the right-hand side, I'm gonna tell it that it's gonna be the Property class again. And this time, I'm just gonna put an open and close parentheses and a semicolon. You do not provide the size of the ArrayList, because it's dynamically grows and shrinks.
Okay, it looks like I have an error. Let's see what it is. "Cannot find symbol: class ArrayList" Oh, I forgot that we have to include the library for the ArrayList. Okay, let's do that next. So we're gonna import java.util.ArrayList. There it is! Okay. That should clear that error up. Now, if we think of the example from before, we created two Property objects. One was for land and one was for an estate. So let's go ahead and do the same thing here, but this time we're gonna add the objects to our mls ArrayList.
I still have to create the Property object. So Property, and we'll just call it "p1" this time, is equal to a new Property and we wanna say the first one we'll make land. So let's say it's worth 10,000 dollars. The type is Land. And the acreage is 3.5. Okay. Now, I can add p1 to my mls ArrayList. So mls. Now notice that it's different.
I don't use the square brackets to get at the elements. Instead, I'm gonna use the .add And in parentheses, I tell it what to add. It automatically assumed I want to add p1 and it is correct, so I'll leave that there. Now I can reuse my p1 property because I already added it to my ArrayList. So now I can say, p1 = new Property and I can create a new property. This time we'll make it an estate. Let's make it 230,000, comma, it's an "Estate." It is on 2 acres.
It has 2 bathrooms and 3 bedrooms. Okay, and again, I can add this one to my ArrayList. Now I have two properties inside the mls ArrayList. When you're working with arrays, as I stated, to access the elements of an ArrayList is different than accessing the elments in an Array. If you wanted to get at an individual element, you use the .get method. If you wanna change an element, you use the .set method.
If you wanna get rid of an element, you use .remove, and we already saw how to use .add Okay, let's print out our ArrayList. I'm gonna do mls.toString() If I run my program the way it is, we can see that it'll print out these two properties. Asking price is $1000 for land, the acre size is 3.5, no baths, no bedrooms. The asking price of the second property is $230,000, it's an estate with a lot size of 2 acres, 2 baths and 3 bedrooms.
Okay, if I go back up to my program, let's try this. Let's remove one of the items from our list. I'm gonna use mls.remove and in parentheses, I'm gonna tell it to remove the one at the index value of 1. Do you know which one it's gonna remove? Well I need to print the ArrayList back out again so I can see which one is missing. So I'm just gonna put the word "After" here so we know which listing is before and which listing is after. Okay, let's try it again. You can see that Land is still there.
Remember ArrayList, just like an Array, position 0 would've been Land and position 1 was Estate. So I deleted the Estate. When trying to decide whether to use an Array or an ArrayList, consider the type of data you need to keep track of. If it is susceptible to change, you might wanna use an ArrayList. If it is mixed data types, then you have to use an ArrayList. Okay, well I hope this cleared up the difference between Arrays and ArrayLists.
- Downloading and exploring NetBeans
- Understanding Java basics: data types, strings, arrays, and more
- Controlling flow with functions and loops
- Creating classes
- Sorting and searching arrays
- Manipulating files
- Handling errors
- Building GUIs