Hello guys, this is Lakshay from Simply Learn and welcome to this tutorial on Arrays in Java. This is a part of our Java tutorial series where we will be covering everything you need to know to master Java. This is what we are going to learn today.
First, we will talk about arrays in Java, the types of arrays and the respective examples. And finally, we will learn about sorting and searching in arrays. We'll learn each topic using a Java code on IntelliJ.
So now, let's talk about arrays in Java. An array is a data type that contains homogeneous elements. Homogeneous means all the elements in the array are of the same data type.
An array is a group of like-typed variables that are referred to by a common name. On screen, you can see the syntax for array in Java. Now let's have a look at an example. All the components of this array are of the same data type, integer.
Each of these elements has an index value associated with it. The index is the position of the elements inside the array. The index of an array always starts with 0. Now let's talk about features of an array.
In Java, arrays are dynamically allocated. This means the compiler does not need to know the exact amount of memory required for the program in advance. Next, all elements are stored under one name.
This means that the elements of an array are known by their array name. Lastly, array occupies contiguous memory location and array is stored in adjacent memory locations unlike linked lists now let's have a look at an example using java code on how to declare and use arrays let's create a new project next tick the box let's name it as array program our class main is created and let's write a code for arrays first the syntax we use in java the data type int bracket and name of the array let's take arr now we have to give size of the array the name of the array is equal to new int and the length you want for the array let's take it as 5. so now let's give values to the elements of the array as the array starts with 0 i'm going to assign 0 and then give a value 10. similarly for the index 1 i'm going to give value as 20. for index 2 30 for index 3 40 and lastly for index 4 50. now let's print the elements of the arrays for this i'm going to use for loop for int i is equal to 0 as the index starts with 0 i less than array of length and i plus plus now let's print the statement using System.out element element at index. Let's print the index first Index is I and next is is and the value of the element Arr of I now let's run this program This might take a while as you can see the program has executed successfully and the result the value of the index and the value of the element is shown 0 to 10 till 4 to 50. Now what if we put an extra element? Let's try it out.
We have five elements and the next element I'm going to put as 5 and give it as 60 and I'm going to run it shift F10. So this gives an error. As you can see the error was out of bounds exception. and it gives 5 because the length of the array was 5. Now that we have seen a basic program of array, let's move on to the types of arrays and let's look at examples using them. We have three types of arrays.
First, we have a one-dimensional array. It is also known as linear array and the elements are stored in a single row. As you can see the example above, the name of the array is array with data type integer.
5 indicates that the array can store five elements. The index goes from 0 to 4. We have a two-dimensional array. Two-dimensional array stores the data in rows and columns. If you can see the example, the array has two rows and five columns. The index starts from 0,0 in the left upper corner to 1,4 in the right lower corner.
And lastly, multi-dimensional arrays has combination of two or more arrays or nested arrays. Now let's have a look at an example of one and two-dimensional arrays using java code. Let's create a new project.
Next. tick the box next and let's name it as dimensional arrays now we have a class main the class main there are two ways we can assign array one i have done before this is the second time you can use to assign values declare and define array we use two flower brackets and write the values inside it separated by commas and then semicolon this is how we declare array in the starting now we need a sum so that we can have some of the arrays assign it as 0 and then we are going to use for loop to get the sum of all the arrays for int i is equal to 0 i less than array length and i plus plus now we are going to get sum of arrays sum plus is equal to array of i now let's print the sum of this array system dot out dot println and write something sum of array is and print array sum and let's execute it and yes we have our output this is sum of array is 15 if you add 1 2 3 4 5 you'll get the sum as 15 this 1 2 3 4 5 now let's delete everything and try to do two-dimensional arrays for this we use int array name let's call it as arr and two brackets now we use a flower bracket and one more flower bracket for rows in Rows I will type it as 1, 2, 3. In second row 4, 5, 6. So with each bracket rows are divided and the elements inside the rows are known as columns. Now we use for loop. We are going to use two for loops. First one is int i is equal to 0, i less than 2 because there are only two rows and i plus plus.
I need to change the bracket. Yeah and declare one more for loop int j is equal to 0, j is equal to 0, j less than 3 because there are 3 columns and j++. Yeah and let's now print the values system.out array of i and array of j and give it a space. Now I need one more line to get to the next line after printing one row. So for the next line I am going to use system.out.println.
And I'm going to delete that ln before. And now let's execute it. Here we have our result. 1, 2, 3 are in a row.
4, 5, 6 is the second row. So now let's try it with multidimensional. What if I put one more row inside it? 7, 8 and 9. So that means I have to change the value for i. i less than 3. Because now we have 3 rows.
And now let's execute it. And yes we have our result. We have 3 rows now.
1,2,3,4,5,6 and 7,8,9. I hope you like this demo. Now let's move on to the next topic sorting and searching for arrays. Now let's talk about searching and sorting using arrays.
First let's understand sorting using an example. We have an array of seven elements from index 0 to 6. The value of elements are given in random order. Then we use sorting algorithm to sort the array in ascending order.
When the algorithm is applied you can see the result on the screen. All the elements in the array are sorted in ascending order. This is how sorting works. Now let's move on to searching in arrays. In this example, again we have an array of 7 elements.
Suppose we have to find the element 11 in this array. We execute a searching algorithm to find the element. You will see the result something like 11 number found at index 5. So this is how searching works in normal array. So now let's see examples of sorting and searching using java code.
So there are many types of searches. But for this program we are going to use binary search algorithm. So let's create a new project. New project next tick the box next and let's name it as searching.
We have our main program here. Now let's create a new class. Right click new Java class and name it as binary search. We have a new class here. So first I'm going to create a constructor sorry int binary search.
Now what is binary search really? Binary search compares the target value to the middle element of the array. If they are not equal the half in which the target cannot lie is eliminated.
If they are not equal the half in which the target cannot lie is eliminated and the search continues on the remaining half. Again taking the middle element to compare the target value and repeating this until the target value is found. So for this I need int array, int left l, int right value r and the element I want to find int x.
So sometimes there are only one element in the array. So to make sure there are two or more than two elements in the array, we are going to use the right element is greater than one. If right r is greater than equal to one, then the code starts.
We need a mid value to get the middle element. int mid is equal to, we have to calculate it, left plus right minus one divided by two. one more bracket there and what if the middle element is the element we are trying to search for then if ARR of mid is equal to X then I'm gonna return mid this means I'm gonna return the value of the mid now what if the element we are searching for is not the middle element then if ARR of mid yes is greater than X then I'm gonna return the same program the same constructor binary search of arr left and now the right becomes mid minus 1 and the limit i want to find x if arr of mid is less than x for that i'm going to write this sentence return binary search of array now in this case left becomes mid plus 1 right remains same and and the element x now i need to return a value for this constructor because the data type of this instructor is int i need to return some integer value for this return minus 1 i think i need to delete one bracket this bracket here now this class looks fine let's move on to main class in main class first i need to take the scanner so i'm going to import java dot util dot scanner so i'm going to write the code starting with I'm going to create an object for scanner so that I can scan all the elements that the user inputs scanner s is equal to new scanner system dot in and next I'm going to create object for the class binary search so binary search OB is equal to new binary search and yes I'm going to declare one array now int arr is equal to new int let's take the length of array as 5 now I'm going to print a message for the users so that they can put the input system.out and the message let's type enter the elements of the array in ascending order ascending order is essential for this algorithm so that we can search easily and this search is properly based on this you Now to get the elements I am going to use for loop for int i is equal to 0, i less than array dot length and i plus plus. Now to take the element arr of i is equal to s dot next int. Now that we have taken the value from the user, now let's ask the user which element they want to find.
For this I am going to print the sentence system dot out, enter the elements. you want to search for now I'm going to declare one more variable num is equal to next next int one more variable to get the length of the array int n is equal to arr dot length now one more variable so that we can store the result of the class int result is equal to ob object of the class binary search dot binary search and the element says arr left would be 0 right will be n minus 1 length of the array minus 1 and the element we want to find num now if the result of the array is equal to minus 1 the result that we passed in our class binary search is equal to minus 1 then i'm going to print element element not present and if the result is not minus 1 then I'm gonna print the value of the element as out element plus the number of the element name and you M num plus and the index of it and the index will be stored in the result now let's run this program and we have successfully executed it there are no errors in the program I'm gonna put the elements now I have put 5 elements 1 to 5 and to search for 3 and yes I can see the result element 3 found at index 2. And that's true, index starts from 0, so the element will be found at index 2. So, this was it for the binary search algorithm. You can try this method or you can try with different elements, anything you want to do with this program.
And now, let's move on to the next program for our searching algorithms. There are many sorting algorithms, but for this program, we are going to use bubble sort. So, let's get started. Let's create a new project.
Take the box, next. Let's name it as bubble sort. We have our class main.
Now let's create one more class. And I'll name it as bubble sort. A bubble sort is an internal exchange sort. Instead of searching an array as a whole, the bubble sort works by comparing adjacent pairs of objects in the array.
If the objects are not in the correct order, they are swapped so that the largest of the two moves up. So this is how bubble sort works. Let's try to do coding on this. I'll create a new class or mainly a constructor with void bubble sort and parameters I'm going to give as int array.
Now I'm going to use n as getting length of the array. For this I'm going to require two loops two for loops nested loops one for i and one for j. So for this for i I'm gonna go int i is equal to 0 i less than n minus 1 and i plus plus I'm gonna use one more loop j j is equal to 0 j less than n minus i minus 1 and j plus plus I have to assign int data type and yes and next I'm going to use if loop if array of j arr of j is greater than arr that means we are comparing two adjacent sides then I'm going to assign one temporary variable and the temporary variable will contain arr next I'm going to swap j to j plus 1 array is equal to array and lastly array will contain the value that is there in temp error of j plus 1 is equal to temp so this will be the code for our bubble sort algorithm and next i'm going to create one more next i'm going to create one more function to print the array i'm creating this function to print the sorted array the same thing i'm going to assign n to array of length and then i'm going to type the message sorted array And next one more for loop to print all the elements of the array. For int i is equal to 0, i less than, n, i++. Now system.out for printing the array, arr of i and one space.
And next system.out.println for the new line. So this created our bubble sort class. Now let's move on to... main class first I'm gonna import the scanner to get the value from the user so import Java dot util Java dot util dot scanner and now we move on to the new code first the object of the scanner new scanner system dot in and now I'm going to create an object for bubble sort ob is equal to new bubble sort so ob is the new object created for bubble sort class now i'm going to assign array and new integer for now let's take it as five and now message to enter the elements enter the elements now i'm going to use one more for loop this is to get the values of the array the same loop i less than array dot length i plus plus arr of i is equal to s dot next int now i'm going to run the program the class that we use in bubble sort ob dot bubble sort of arr and now to print the message this message is to print that all the elements are sorted in the array this is mostly used to make it as user friendly so i'm for printing array one more function and now let's run the program and yes we have executed successfully this program i'm going to enter the elements 1 5 9 2 4 and yes we have the sorted array all the elements before were in not in order and now all the elements are in ascending order similarly we can do with descending order also if we put i as the maximum and then reduce i and j you by using i minus minus or j minus minus so this brings us to the end of this video we learned about arrays the types of arrays and sorting and search and again so this brings us to the end of this video we learned about arrays types of arrays and searching and sorting in arrays i hope you like this video thank you for watching and stay tuned for more from simply learn Hi there, if you like this video, subscribe to the Simply Learn YouTube channel and click here to watch similar videos. To nerd up and get certified, click here.