The course is part of this learning path
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.
- 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
This is a beginner-level course and can be taken by anyone with an interest in learning about Java.
In the previous couple projects, we grow our skill set with working with Arrays and ArrayLists. Now, you're going to put some of your knowledge to work by creating two Arrays or ArrayLists. You can choose and fill them each with data corresponding to the other. Here's how it will work. You will create a Proj4_3_Parallel file and fill it with your regular skeleton program. Then, I'd like you to take in input from the keyboard to store the names and ages of five individuals. You will need two arrays or ArrayLists to store that data.
One will hold names and the other will hold ages. However, the element in each array or ArrayList at a given index, say, index 2 will refer to the same person. Once you've taken in the five people's names and ages and stored them in these so called parallel Arrays or parallel ArrayLists, print out their name, followed by a statement saying their age such as, "Bob is 15 years old." Do this for every one of the five people in your Arraylists and then the program will end. As a little bit of a hint, don't forget about what happens when you take a non string as input, followed by a string. This happens in loops as well. So, remember our little magic trick consuming the new line character that might help you out here.
All right, so here's a demo of our project 3 in section 4 project. Right click, 'Run', enter a name, we'll say, John who is 27, Bob is 30, Sally is 21, Ellen is 77, and Dan is 40 and of course, it responds and we get all of these sentences here and remember it's storing each of the corresponding string values in one array and the integer values here in another. Hope that helps. So, pause the video and give this one your best shot. Come back when you've completed this project or if you need some help. How did this project go for you?
This one could have been a little challenging. Using parallel arrays or ArrayLists isn't difficult, but if you've never done it before, it might seem a bit confusing at first. Let's work on this together. So, we're going to right click here 'New', 'Java class' and we'll call it Proj4_3_Parallel right there. We need the scanner and the Arraylists. So, import java.util.ArrayList. And there we go. Okay, there's our little skeleton. Now, we get the scanner ready to take an input from the keyboard through the console. Now, I need an ArrayList of strings. Remember we need two of them. You could have used an array. That's fine too. Names = new ArrayList<>(). Okay. There's one and then, we need an ArrayList of integers for the ages but remember we can't use the primitives. We have to use the corresponding Wrapper class. Here we go. All right. That's half the battle right there. I'm going to also use just to make things a little easier, some separate little variables here for us to take in the initial input. All right. There we go.
String, name and int age. Notice that there's names and ages. Those are separate variables from this. And we've got i = 0;i<5 because we want to take in five people, system.out.print("Enter a name:\t"); Follow that tab = keyboard.nextLine() so that's a string, and then we say enter and age and this one is an integer. So far, so good. However, once we enter the integer, there will still be, once we hit Enter, it will go next and it will loop around again and then it will grab the new line or the Enter key that we hit and think that that's the next name.
So, we want to avoid that by consuming the new line character that we learned about earlier in the course. That's the consumption of the new line right there. And now, we want to store the values in our data structure. Specifically, we have an ArrayList. So, if you have an array, you'd say name at and then you have to keep track of your counter here. But for us, we have an ArrayList ages.add(age), names.add(name). And then, maybe if we want a little extra space because there's a whole lot going on here, we'll put a print line at the end. Otherwise, it looked crammed together and then we want to print out some statements. So that's what we've got so far.
We want to print out the statements after this for loop, we have another for loop, i< names.size(). And again, you could use the enhanced for loop, if you'd like. Here's our other for system.out.print line names.get(i). So, that will return the string with the name. And I'm going to put a space, is, another space and we'll go to the next line, ages.get(i). So, the corresponding age for that name, and then space years old. There we go. And of course, as always, let's run it to test it out. Right click. Run it. Here we go. Enter a name. Let's say, John and he's 37. Looks like I didn't keep that consistent. I'm going to actually kill this process and go back up and I did println instead of print, so it was my fault.
Let's run it again. All right, there we go. Enter a name. We'll say John. He's 37 and it gives us a little extra space. We'll say Kathy is 22, Billy is 14, Stephen is 20, and Sally is 47. And there we go. We've got quite a bit going on here. John is 37, Kathy is 22, Billy is 14, Stephen's 20, Sally's 47 so it looks nice and seamless, but there's a lot going on here. There are actually two ArrayLists, two data structures that we're using at the same time. It's really, really cool. So, good. We got the output that we expected and wow, that was a lot of code. Really. Great work, everyone. In the next lecture, we'll end this section with our section wrap up. I'll see you there.
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.