Project - Working with ArrayLists

Contents

keyboard_tab

The course is part of this learning path

Start course
Overview
Difficulty
Intermediate
Duration
57m
Students
100
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

The previous lecture served as your first project, where you were able to test out your skills using a regular built-in array. In this lecture, you will have your second project in which you will test your abilities with the ArrayList class. For this project, you'll create a file named Proj4_2_ArrayLists. And you'll use an ArrayList which you will populate with non-negative numbers from the keyboard that the user will enter. Remember, non-negative numbers in this context means real numbers, zero or greater. 

You should only stop asking for numbers when the user enters a negative number, which will signal that they are done. When the loop is finished, you should print the values in reverse order from what was entered. Decide carefully which repetition control statements might be best for this project. One for input of the values and one for output of the values, and they don't have to be the same repetition control statement. Additionally, you might consider doing a priming read before the loop begins. For some hints, remember that the ArrayList can only hold reference types but that we have the wrapper classes that can help us.

Also, think about what kind of method you would need with the scanner that easily read in double data. And finally, since you have to print the ArrayList contents in reverse, you have to start at the last element and go down. So, how might you accomplish this? Can you use the size method to your advantage? All right. So, let's demo the 'Proj4_2_ArrayLists right here, right-click, and then we have 'Run'. And we enter some values, and this time it's using sentinel control. So, if we put say any number bigger than zero, it will keep going, and as soon as we get a negative number it'll stop. So, let's say 175.75 and 10.5. And remember the goal is it will print it backwards. So, the -1, and there we go. We've got 10.5, 177.5, 222.2, and 199.88. So, there we go. So, pause the video, come back when you're done or if you need some help.

How was this project for you? Were you able to complete it? There were a couple of things that might have thrown you off a bit. So, this one was more of a challenge than the last project I think. So, let's work on it together. Let's create the file. Right click, 'New', 'Java Class', and it will be called 'Project4_2_ArrayLists'. There we go. And we know that we need java.util.ArrayList, of course. And also java.util.Scanner. And we should put the main method in here, public static void main(String[] args). And we need a scanner, of course. So, let's do that. In an equal sign, I don't know, I think I didn't hit the key hard enough. There you go. System.in, and then we have the ArrayList and since we have to hold real numbers, I want to use the double type. So, double myList = new ArrayList<>(); And now let's have a double input. And we're going to do a priming read before the loop. So, system.out.println ("Enter 0 or more to put in the list", and then maybe I'll say concatenate "or -1 to exit").

We don't have to tell them that they can enter any negative number. You can just say, "Hey, hit a negative one", because that gives them too many options, and they might hesitate, input = keyboard. Now, we've used nextInt before, but you'll notice the intelligent fill right here is showing us nextDouble. So, we could use nextDouble right there. That should be good. And, of course, we need after that to have a while loop. So, while that input is greater than, oops, sorry, input >= 0. And this is okay in this case to compare the real number to an integer because you're not going to lose data, and they can be compared like that. All right. And we need to add the information we just got from the priming read and then each subsequent iteration. System.out.println9, and then actually, I'm going to actually copy this, save myself a little bit of time right there. Here we go. Cool. And then after that loop is done, we're going to print all of the values in reverse.

Now, this is a little bit more challenging because we haven't really done this before, but with a loop we can, of course, start at any value we want and make a test in the middle for the loop continuation, and then we can go in whatever direction we need to up or down and buy whatever values we want. We just normally start at zero and gone up by one each time, up until like a size or something like that. But we can go down. So, we can say myList.size() -1. So, that would be the last element, as long as i >=0 and then i--. So, we're going down this time. That's reverse. All right. Now, let's print it out, println(myList.get the value at i. And, of course, let's run it. And let's do 3.14, 4.9 rather, 5.18, 7.75. And we'll just put 10 and then put -1 to exit. And there we go, it goes in backwards order. Right here, you'll see. Pretty cool. So, that's a pretty cool little program. You may have completed it with no problem or maybe you found it challenging and finally finished it. Or maybe you weren't even sure where to start.

All of those are totally cool. If you completed it, congratulations. That one was a little tricky. If you didn't figure it out, don't sweat it. Maybe you didn't realize that we could do more than just go up by one in a for loop. In other words that we can go down or do pretty much anything with our loop continuation variable i as well. Again, this is a marathon, not a sprint. The more you are exposed to problem solving in general and Java in specific, the better you will become at these challenges. I didn't explicitly teach you all of those pieces put together, which is why this project was a bit more challenging. That's largely what programming is all about. Putting together the pieces you do know to solve problems that you haven't solved before. In the next lecture, we end our projects with a parallel array project. I'll see you there.

 

About the Author
Students
798
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