Searching ArrayLists

Start course
Overview
Difficulty
Beginner
Duration
2h 16m
Students
26
Description

In this course, we'll learn the Collection framework and the Map Interface.

Learning Objectives

  • Collection Framework vs Array
  • Hierarchy of Collection Framework
  • List interface - ArrayList Class
  • Iterator Interface
  • Set Interface
  • Queue and Dequeue Interfaces
  • Map Interface

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

Prerequisites

  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge
Transcript

Hi there. In our previous lesson, we made examples about ordering the elements in an ArrayList. In this lesson, we'll learn another related topic, Searching ArrayList. Again, the logic in this issue is the same as the logic we use in arrays, only the method used is different. For lists, we use the binarySearch method of the collection class. If you want, let's remember the logic here once again. Of course, first of all, I should point out that your list must be sorted, otherwise we get unpredictable results. In the example on the slide, we have an integer list called age and the elements of this list are sorted. If we want to find out the index number of the element 14 in this list, we can use the Collections.binarySearch method. This method takes two parameters. The first is the name of the list and the second is the element whose index number we want to know. 

And as output, we get the value 2, because the index number of element 14 is 2. Yes, there is no problem here. Well, if the element we're looking for is not in the list, for example, what if we want to learn the index number of element 15 in the same list this time? In this case, it outputs -4. Now, let's look at why? First, in a sorted list, the result is always negative when the element we are looking for is not in the list. Therefore, the result is minus. So why -4? Let's look at this. If 15 is not an element of this list, then it places 15 in the most appropriate place in this list, i.e., just after element 14 and just before element 16. In this case, the element number of 15 will be 4, but it will always give the results as negative because something went wrong and such an element doesn't actually exist in this list. Yes, as a result, when you come across a question about searching list, first answer the question, "Is the list a sorted list or not?" If it is not a sorted list, the result is unpredictable. If it is a sorted list, then answer the question, "Is the searched element an element of the list or not?" 

If it's not, then the result will be the negative value of the index number after inserting the element into the list. If the element we are looking for is an element of the sorted list, then the output will be the index of the element. Now, let's move on to the Eclipse and get some practice. First, I'll create a class. I'll right click on the collections example package and select the new class options. So, the name of the class can be SearchingArrayList, and check the checkbox for the main method and click the Finish button. Okay, first let's create a not sorted integer array list, ArrayList(integer). The name of the list can be age = new ArrayList. Let's add some elements to this list. The first element can be 16. Now let's copy and paste this list six times. The second element can be 8, the third can be 14, the fourth can be 12, the fifth can be 36, the sixth can be 24, and the last can be 19. 

Okay, now, first let's search the element 14 in this list. For this, I'll use the binarySearch method of the collections class. So, I write collections.binarySearch. The binarySearch method will take two parameters. The first parameter will be the list name, i.e., age. The second parameter will be the element whose index number we want to know. So, I write 14. Of course, to observe the result on the console, let's move this method into the println method. I will cut this method and call the println method and paste the binarySearch method inside the parentheses. So, let's guess the output. First, I'll look at the sorting of the elements. So, this list is not a sorted list. Okay, so the output is unpredictable. Let's run and see. As you can see, the result is different. Now, before searching, let's sort the list. Here, I write Collections.sort(age). So, now the list is sorted. The result is predictable. Second, I will look for the element 14 in this list. Now, let's write the sorted list as a comment here. 

So, since the element we are looking for is an element of the sorted age list, the output will be the index number of element 14, i.e., 2. Let's run the code and see. As you can see, the output is 2. Now, let's search for another element. I'll copy this code and paste it below. This time, let's search the element 24. This time the output will be 5 because the list is a sorted list and the index number of 24 is 5. Let's run the code. As you can see, the output is 5. Now, let's search for an element that is not in the list. I'll copy and paste this line. The element can be 5. Now, let's guess the result. The list is a sorted list and its outcome can be predicted. But the element we're looking for is not in the list, so the result is definitely negative. Now, let's put this element into the list. Notice that if we try to include 5 in this list, it will be the smallest element in this list. So, five's ordinal number in this list will be 1. So, the results will be -1 as a negative value. Let's run and see the output. As you can see, the output is -1. Yes, I think you got the logic. As I said at the beginning of the lesson, the logic is the same as in arrays. That's why I don't want to get too repetitive. Let's take a short break now. See you in the next lesson.

 

About the Author
Students
409
Courses
35
Learning Paths
2

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics