The course is part of this learning path
In this course, we'll learn about Arrays in Java.
Learning Objectives
- What is an Array?
- Declaration and Initialization
- Sorting Arrays
- Searching Arrays
- Multi-Dimensional Arrays
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
Hi there. In our previous lesson, we made examples about ordering the elements in an array. In this lesson, we'll learn another related topic, Searching Arrays. So, what is a searching array?
Sometimes we have an array with too many elements and we may want to know at which index a particular element is in this array. Of course, you can write different methods to detect this. But just like the sort method we learned in the previous lesson, there's a method developed for this purpose, the binary search method. This method is also a method of the arrays class. Now, let's try to understand this through an example. Of course, first of all, I should point out that your array must be sorted, otherwise we get unpredictable results. In the example on the slide, we have an int array called age, and the elements of this array are sorted.
If we want to find out the index number of the element 14 in this array, we can use the Arrays.binarySearch method. This method takes two parameters. The first is the name of the array and the second is the element whose index number we want to know. And as we output, we get the value 2 because the index number of element 14 is 2. Yes, there is no problem here. But what if the element we are looking for is not in the array? For example, what if we want to learn the index number of element 15 in the same array this time? In this case, it outputs -4. Now let's look at why.
First, in a sorted array, the result is always negative when the element we are looking for is not in the array. Therefore, the result is minus. So, why -4? Let's look at this. If 15 is not an element of this array, then it places 15 in the most appropriate place in this array 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 result as negative because something went wrong and such an element doesn't actually exist in this array. Yes, as a result, when you come across a question about a sorting array, first answer the question, is the array a sorted array or not? If it's not a sorted array, the result is unpredictable.
If it is a sorted array, then the answer to the question, is the searched element an element of the array or not? If it's not, after placing this element in the array, whatever the sequence number is, the result will be the negative of that number. If the element we are looking for is an element of the sorted array, 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 array package and select the new class options. So, the name of the class can be SearchingArray. And check the check box for the main method and click the 'Finish button.' First, let's create a sorted int array. The name of the array can be age.
The elements of this array can be 8, 12, 14, 16, 19, 24, 36. Now first, let's search the element 14 in this array. For this, I'll use the binary search method of the arrays class. So, I write Arrays.binarySearch. As you can see, you can search all arrays type such as, byte, char, double, int, object, et cetera. Let's continue. The binary search method will take two parameters. The first parameter will be the array 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 results on the console, let's move this method into the println method. I'll cut this method and call the println method and paste the binary search method inside the parentheses. So, let's guess the output.
First, I'll look at the sorting of the elements. So, this array is a sorted array. Second, I'll look for the element 14 in this array. So, since the element we are looking for is an element of the sorted age array, 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 array is a sorted array 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 array. I'll copy and paste this line. The element can be 5. Now, let's guess the result.
The array is a sorted array and its outcome can be predicted, but the element we're looking for is not in the array, so the result is definitely negative. Now, let's put this element into the array. Notice that if we try to include 5 in this array, it will be the smallest element in this array. So, five's ordinal number in this array will be 1. So, the result will be -1 as a negative value. Let's run and see the output. As you can see, the output is -1. Let's search for an element that is not in the array again. I'll copy and paste this line. The element can be 30. Now, let's guess the result. The array is a sorted array and its outcome can be predicted, but the element we're looking for is not in the array, so the result is definitely negative.
Now, let's put this element into the array. Notice that if we try to include 30 in this array, it will be 24 and 36. So, 30's ordinal number in this array will be 7. So, the result will be -7 as a negative value. Let's run and see the output. As you can see, the output is -7. Yes, I think you got the logic. If you want, let's do an example for the string array. Let's create another array. Let the array name be cars. Let the elements be BMW, Ferrari, Ford, Mercedes, Opel, in order. Now, let's search for the BMW element using the binary search method. S out Arrays.binarySearch. The name of the array is cars, and the value will be "BMW." Okay, let's guess the result. The array is a sorted array and the BMW is an element of this array.
So, the output will be the index number of the BMW i.e., 0 because the BMW is the first element of this array and the index of the first element is 0. Let's run the code and see. As you can see, the result is 0. Now, let's search for another element. I'll copy and paste this line. The element can be Volkswagen. Now let's guess the output. The array is a sorted array and its outcome can be predicted, but the element we are looking for is not in the array, so the result is definitely negative. Now, let's put this element into the array. Notice that if we try to include Volkswagen in this array, it will be the last element in this array.
So, Volkswagen's ordinal number in this array will be 6. So, the result will be -6 as a negative value. Let's run and see the output. As you can see, the output is -6. Yes, this is how we search in arrays. Things to keep in mind. First, check if the array is sorted. If it's not sorted, the result is unpredictable. If it is sorted, check if the element you are searching for is present in the array or not, and if the array exists, the result is the index number. If it's not present in the array, the result is always negative. In addition, after placing this element in the array, whatever the sequence number is, the result will be the negative of that number. Let's take a short break now. See you in the next lesson.
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.