Using ArrayLists

From the course: Learning Java 8

  • Course details

    Java is one of the top-five programming languages, and is used for websites, embedded controllers, and Android app development. This is an introduction to get you started programming with Java. Peggy Fisher introduces the basics: data types, strings, arrays, expressions, loops, and functions. She'll help you control the flow and logic of your code, and create classes using the principles of object-oriented design. Then go a bit beyond the basics and learn advanced techniques for working with arrays, manipulating files, and building graphical user interfaces (GUIs) that respond to user input. This three-hour capsule course is perfect for developers who need to get up to speed with Java fast, as well as for beginning programmers who want their first taste of this popular language.

    Instructor

    • Click here to view Peggy Fisher’s instructor page

      Peggy Fisher

      Content Strategist, Software Development Languages at Linkedin Learning Solutions with Lynda.com content

      Peggy Fisher is a programmer and content manager at LinkedIn Learning.

      Peggy's main focus is application programming in Java, Arduino, and C++. She has also worked on courses in COBOL and discrete ,athematics. Previously she was a faculty member at Penn State University's College of Information Sciences and Technology. She started out as a programmer working for a large insurance company, but after 18 years she left her job as a director of information technology to pursue her true passion teaching. She earned a master's degree in math education, and went on to teach high school math and computer science in Pennsylvania. In 2012, Peggy accepted a position as an instructional designer at Penn State, and shortly thereafter began teaching Intro to Application Programming with Java. She is a strong supporter of women in STEM. As one of the few female programming teachers, she serves as a mentor to incoming female freshmen who are considering a career in programming. She was also the K–12 outreach coordinator for the college, where she scheduled, ran, and taught summer camps for middle school and high school students. In a PBS NewsHour interview, she expressed that all students should take at least one programming class either in high school or college. Peggy enjoys constantly learning and finding new and exciting ways to bring technology to life in and outside of the classroom, such as using Arduino microcontrollers or Lego Mindstorms, to help make learning hands-on and fun.

    Skills covered in this course

  • Welcome

    - 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.

  • Practice while you learn with exercise files

    Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.

    Download the exercise files for this course. Get started with a free trial today.

  • Download courses and learn on the go

    Watch courses on your mobile device without an internet connection. Download courses using your iOS or Android LinkedIn Learning app.

    Watch this course anytime, anywhere. Get started with a free trial today.

Contents