Session 7- Working with Java Arrays | Coding Examples o… — Transcript

Learn Java array basics with focus on searching and sorting algorithms like linear search and bubble sort for interview preparation.

Key Takeaways

  • Understanding the logic before coding is crucial for solving array problems.
  • Linear search is a fundamental algorithm to find elements in an array.
  • Sorting and searching are common interview topics with multiple algorithmic approaches.
  • Breaking loops early improves efficiency when the target element is found.
  • Practice with both classic and enhanced for loops to implement array operations.

Summary

  • The video continues the topic of working with Java arrays, focusing on practical examples.
  • It emphasizes the importance of searching and sorting algorithms in arrays for coding interviews.
  • Linear search is introduced as the simplest and most popular searching algorithm.
  • The instructor explains the logic behind searching an element in an array before coding.
  • Sorting algorithms such as bubble sort, insertion sort, and merge sort are mentioned.
  • The video highlights the step-by-step approach: understanding the problem, framing logic, then coding.
  • Examples include searching for elements like 100 or 700 in an array of numbers.
  • The concept of breaking the loop once the element is found to optimize searching is explained.
  • The difference between searching for an element and handling duplicates is clarified.
  • Both classic and enhanced for loops are suggested for implementing search logic.

Full Transcript — Download SRT & Markdown

00:03
Speaker A
So in our previous class, we have seen how to work with arrays, single dimensional and two dimensional. Today, we will continue the topic. We will see different types of examples and how we can solve different types of problems using arrays.
00:21
Speaker A
important uh programs is searching and sorting so when I suppose if anybody ask want to ask any question from array con ccept definitely they will ask about sorting and searching searching and sorting means array contains some of the values and we have to print them in the sorted
00:39
Speaker A
So the most popular and most important programs are searching and sorting. Suppose if anybody wants to ask any question from array concept, definitely they will ask about sorting and searching.
00:51
Speaker A
of the times people will ask in interview and at the same time there are so many Logics are there so there is not only one or two ways there are so many almost 10 to 15 different ways are there and
01:02
Speaker A
Searching and sorting means the array contains some values, and we have to print them in sorted order. Sometimes, if you want to search an element in an array, we should be able to find or search a value in an array.
01:21
Speaker A
least just learn one process the simplest way how can we sort and such an element that is more than enough okay so later if you want to learn each and everything I will share some of the links and you can learn yourself but initially whatever is important as for interview so I will just try
01:39
Speaker A
These are the popular programs most of the time people will ask in interviews. At the same time, there are so many logics. There is not only one or two ways; there are almost 10 to 15 different ways.
01:57
Speaker A
algorithm there are different type of algorithms this is the most popular one so linear search okay now we'll see how to search an element in an array it's very simplest first we need to understand the logic so whenever you see problem statement first time just read multiple times and once
02:15
Speaker A
Searching and sorting elements: if you look at searching, we have linear search, binary search. Similarly, if you want to sort, we have bubble sort, insertion sort, merge sort, okay? There are so many different types of algorithms.
02:35
Speaker A
Array first let us think about the logic so don't directly go to program part and first let us see how can we search an element so if I give some elements in Array how can we do the search if
02:46
Speaker A
Initially, we don't need to learn everything. At least just learn one process, the simplest way how we can sort and search an element. That is more than enough.
03:01
Speaker A
let us say randomly 300 500 100 200 600 like this now my search element is let us say 100 100 is my search element now if I ask you this 100 is present in this array or not immediately
03:18
Speaker A
Later, if you want to learn each and everything, I will share some links and you can learn yourself. But initially, whatever is important for interviews, I will just try to cover those parts.
03:34
Speaker A
on what basis immediately you can find right so 100 is nothing but 100 is a number if I look at here in AR there is an element 100 so immediately can say 100 is present 700 is not present in you
03:47
Speaker A
First, let us see how we can search an element in an array. For example, one search an element in an array, and basically this is a linear search. This is a linear search algorithm. There are different types of algorithms; this is the most popular one, so linear search.
04:00
Speaker A
need to frame that's the first step okay once you understand the logic then you can easily implement the program so let me tell you the pro solution here then we'll try to implement the first step whenever you want to find a number present in this array or not first we have to read
04:18
Speaker A
Now we'll see how to search an element in an array. It's very simple. First, we need to understand the logic. Whenever you see a problem statement for the first time, just read it multiple times. Once you read the problem statement, then frame the logic.
04:37
Speaker A
is happened the exit from the for Loop and tell element found okay element found so what exactly we doing here we are reading each and every value from array that's the first thing by using F we can read right each and every value we can read but as soon as you read a value immediately by
04:58
Speaker A
Suppose if you don't write a program directly, don't write a program. Just think about the logic manually. How can we solve the problem? Then try to put it into the program. This is a step-by-step process.
05:20
Speaker A
have to say element is not matching not for just one number okay so you have to compare this number with all the numbers and if it is none of them are not matched then only you have to say 100
05:33
Speaker A
Search an element in an array. First, let us think about the logic. Don't directly go to the program part. First, let us see how we can search an element.
05:48
Speaker A
number and finally if not matching with any number then only you have to say number is not found okay this is the logic now remember this this we have to read all the data one by one that's the first
06:03
Speaker A
If I give some elements in an array, how can we do the search? If I ask you whether the element is present in the array or not, how can we say this?
06:21
Speaker A
matching is here so 100 is matched here so element already found here so as soon as the element is found then we don't need to compare with the rest of the number so immediately we break the loop right we will break the loop if element not found then we will go to the next number
06:38
Speaker A
Suppose let us say I have taken one array like this, and I'll put some elements in this. I'll store some elements, let us say randomly 300, 500, 100, 200, 600 like this.
07:00
Speaker A
today new package today is day 17 click on fin all right so now inside the day seven let's create a new class and searching me in Array there are many ways to do this but this is one of the popular
07:33
Speaker A
Now my search element is, let us say, 100. 100 is my search element. Now if I ask you, is this 100 present in this array or not, immediately you say yes.
07:53
Speaker A
whether it can be one or it can be duplicated it doesn't matter whether that number is present or not anywhere in in this array even though if you have a duplicates which one is wherever you found the first element you can just match it and then exit from the F rest of the numbers we know need
08:09
Speaker A
If I ask you 700 is present in this array or not, you will say no. So on what basis and how are we saying 100 is present in this array and 700 is not present in this array? How are we seeing?
08:26
Speaker A
wherever it is matched first time we will compare match is over then exit from the for Loop and INB say find the number okay we not concerning about duplicates here we are just checking the element is present or not and finding the duplicate is totally different concept different logic
08:44
Speaker A
On what basis immediately you can find, right? So 100 is nothing but 100 is a number. If I look here in the array, there is an element 100, so immediately I can say 100 is present.
08:59
Speaker A
we'll do step by step listen this very carefully because it is more logic oriented so in the next examples every example is purely logic based okay so you need to carefully focus and listen this so first thing what we need to do is whenever you want to find an element in Array first we
09:21
Speaker A
700 is not present, you can simply say 700 is not present. But how are you saying, and before saying 100 is yes, what exactly are you doing?
09:42
Speaker A
can put whatever numbers you want So currently I'm taking 10 to 505 numbers now what is the element you want to search that also I can take one variable enter search element search element so which element you want to search let's say I want to find 30 is present or not I want to
10:01
Speaker A
What exactly are you doing in this array? That is the main logic which we need to frame. That's the first step.
10:19
Speaker A
Loop so you can write classic for Loop or you can write enhanced F okay anything is fine so which one you will prefer so one F loop I will show you another one you guys can try classic F Loop or enhanced F Loop so most of the times we will follow enhanced fop but I will show you both
10:40
Speaker A
Once you understand the logic, then you can easily implement the program. So let me tell you the solution here, then we'll try to implement.
10:56
Speaker A
me here a do length or a do length minus one a do length or a do length minus one so when I say less than or equal to you have to say length minus one when I say just less than you can say a do length
11:12
Speaker A
The first step: whenever you want to find if a number is present in this array or not, first we have to read the first element from the array and compare it with 100.
11:34
Speaker A
value in every itation so first time I value zero so a of I we will get 10 next round I value one so here A A of one 20 you will get next round I value two then here a of two 20 30 you will get
11:49
Speaker A
If both are equal, that's fine. If both are not equal, then what should we do? Take another element and compare with 100 again. If not equal, then take another number and compare with 100.
12:02
Speaker A
is not our requirement what is our requirement as soon as we get a value we have to compare with our search element right so let's remove the print statement now we need to compare this with our search element how to compare using IF condition okay if if a of I and what is an operator we have
12:26
Speaker A
Here it is matching, and whenever the match happens, exit from the for loop and tell element found.
12:48
Speaker A
out. element found so as soon as element is found you don't need to compare with the next values you don't need to compare with the rest of the values in Array right after element is found so you don't
13:02
Speaker A
What exactly are we doing here? We are reading each and every value from the array. That's the first thing. By using for loop, we can read each and every value.
13:14
Speaker A
you found an element here so you don't need to go with the next value but so now here I can simply say break why I'm saying break here because as soon as you found an element in Array we need we
13:28
Speaker A
As soon as you read a value, immediately by using if condition, compare with your expected number. If the expected number is found or equal, then immediately exit from the for loop and number is found.
13:47
Speaker A
objective here so as soon as you found an element that is found that's it you don't need to compare with the rest of the elements so immediately say break so what this break command will do it will exit from this for Loop or it will break the for Loop okay suppose if it is not matching
14:06
Speaker A
If it is not matched, then go with the next number like this till the end of the array. If none of them are matching, then finally we have to say element is not found.
14:21
Speaker A
found immediately we should not say element not found okay if element is not found immediately we should not say we should also compare with the next values after completion of all the values or after comparing with all the values finally we have to say element not for not with just a number
14:40
Speaker A
Not for just one number. You have to compare this number with all the numbers, and if none of them are matched, then only you have to say 100 is not found.
14:59
Speaker A
for but that is not right right because we want to check rest of the numbers after checking all the numbers then only finally we have to say element not form immediately we should not say so else block we should not put here else block we should not put here okay because if it is matching
15:21
Speaker A
But simply in one statement, suppose 100 is not matched, then immediately you should not say 100 is not present in this array. We should go with the next number.
15:47
Speaker A
exactly Happening Here is if it is not matching then L part will execute again it will go up not matching then again El part for every element we are saying element not found element not f right so we should not say like this after comparison of all the elements if no element is matching finally
16:07
Speaker A
If still not matched, then go with the next number. Still not matched, then go to the next number, next number, and finally if not matching with any number, then only you have to say number is not found.
16:31
Speaker A
in two cases what is the first case if element is matching here it will tell element found and then it will automatically exit okay that is a one case if element is found immediately the break command will come out from F Loop so F Loop will exit that's the one case and what is the second
16:51
Speaker A
This is the logic. Now remember this. We have to read all the data one by one. That's the first step.
17:13
Speaker A
F Loop will automatically exit after condition is true the for Loop automatically exit right so two cases one is after matching is found after element is found it will immediately exit second case is after completion of the all the numbers then it will exit right but which case in which case we
17:36
Speaker A
Parallel, as soon as you read some value, we have to compare with the expected number. If matching, immediately we can say element found and exit from the for loop.
17:54
Speaker A
element not found just observe this okay element not form okay let's try to execute the code and see how it will work run as Java application see what is an output you are getting when I say 30 here the matching is done so element found it is got printed break and it is exited from the
18:17
Speaker A
Because suppose here we found element, do you really want to compare with the rest of the numbers? No need, right? Because already matching is here.
18:36
Speaker A
execute this saying element not found 100 is not there in Array it is saying correctly so why it is saying correctly because after executing the F Loop so there is no matching happened finally F Loop is exiting normally and here it is saying element not found right this is okay fine but when
18:57
Speaker A
So 100 is matched here, so element already found here. As soon as the element is found, then we don't need to compare with the rest of the numbers.
19:17
Speaker A
exiting from the F Loop After exiting the F Loop we should say element not found okay that is fine but on what basis we have to check element not found so we need something a temporary variable we require here because as soon as you match is done you're simply saying element found and After
19:41
Speaker A
So immediately we break the loop, right? We will break the loop. If element not found, then we will go to the next number, next number like this.
19:58
Speaker A
use so Boolean variable we required so Boolean variables will be used to maintain the status or sometimes we maintain the status or Flags Okay so let me take one variable called status and let me Ascend false default Valu is false okay so let us think false means not found true means
20:20
Speaker A
Depending upon the number of values in the array, we will repeat the loop. This is the logic.
20:43
Speaker A
equal to false buan variable now what I will do is as soon as you found element here okay before breaking I will change the status value is true okay I will change the status value true because here we found an element okay as soon as you found an element make the status value true so
21:06
Speaker A
Once you understand the logic, now we will convert this into programming format.
21:19
Speaker A
After exiting the for Loop if the status value is equal to true what does it mean After exiting the for Loop also status value Still Remains the Same right initially it is false and when you are changing the Status value after element is found then only we are changing the status of
21:37
Speaker A
Now let us try to implement the program for that. Go to Eclipse and let's create a new package for today. New package, today is day 17. Click on finish.
21:58
Speaker A
Same same initially whatever value we have taken false After exiting the for Loop also status value is still false what does it mean element not found so that we need to compare After exiting from the for Loop here if status value is still false still it is false then we have to say element not F only
22:21
Speaker A
All right, so now inside day 17, let's create a new class and searching element in array. There are many ways to do this, but this is one of the popular ways.
22:43
Speaker A
so if it is matching here this will not match if this is not matching for every element and this is matched wice Versa okay this is for positive condition this is for negative condition if the status value got changed here this condition Falls obviously right so now you'll get the right output
23:04
Speaker A
Even though if you have duplicate values, no problem. Wherever it is found, the number immediately it will find and then exit from the for loop.
23:21
Speaker A
okay so this is a solution for this so we have to take the help of a Boolean variable somebody asked in the previous sessions where we can use Boolean variables so this is a use case and many places we require Boolean variables and especially while using conditions also these are
23:40
Speaker A
So this is our intention. It is not finding the duplicate numbers. Our intention is just to check that number is present or not.
23:57
Speaker A
the status still false then element not okay so this is how we can do searching search an element in this is called linear searching algorithm so linear means every element we are comparing for every element we are comparing and same thing we can achieve by using enhanced for Loop also it is
24:17
Speaker A
Whether it can be one or it can be duplicated, it doesn't matter whether that number is present or not anywhere in this array.
24:35
Speaker A
the array Name colon and here we need a variable which will hold the values from array 1 by one in some X okay now condition same thing we can compare so let's take this and keep it here so
24:51
Speaker A
Even though if you have duplicates, wherever you found the first element, you can just match it and then exit from the for loop. The rest of the numbers we don't need to compare.
25:13
Speaker A
30 element form perfectly working suppose if you have a duplicates how it will work let me show you let's say I'm taking 30 here and one more 30 so we have three elements 30s are three let's try to execute run as Java
25:30
Speaker A
That's the reason we have to be more clear about the problem statement. Here we are not talking about the duplication of elements. Here we are talking about the searching of an element.
25:45
Speaker A
okay so this is how we can find search an element in a okay suppose if you want to find how many times it is repeated suppose here 30 is repeated three times and 10 is repeated only one time 20
26:02
Speaker A
If you have 100 number, we are searching, and 100 is present three times or two times, wherever it is matched first time, we will compare, match is over, then exit from the for loop and say find the number.
26:26
Speaker A
first example is clear everyone please confirm in the chat box searching an element in a logic you have to be understand first if you not understood the logic you can't understand the problem okay now so let's move on to the next one we have seen how to search an element in Array even though if
26:47
Speaker A
We are not concerning about duplicates here. We are just checking the element is present or not. Finding duplicates is a totally different concept, different logic we need to use.
27:07
Speaker A
statement okay so find number of reputations okay so find the number of reputations so for example first again think about the logic so don't directly write a program first thing about the logic let's say I have an array like
27:34
Speaker A
So this is for searching an element. It can be single or it can be multiple numbers. We don't bother. Finally, we have to say the number is present in the array or not.
27:49
Speaker A
repeated how many tens are there in this array or how many 20s are there in this array I want to verify so initially let me take one number so how many times the 10 is got repeated how we will tell he will compare it right he will take first read one number compare with this
28:07
Speaker A
Now we will do step by step. Listen very carefully because it is more logic oriented.
28:24
Speaker A
becomes three three times it is repeated now one more number is 10 and compare so again matching is happened so then it becomes four four times is repeated and then next number not matched so then exit from the for Loop so in whenever the matching is happened we have to increase number
28:45
Speaker A
In the next examples, every example is purely logic based. So you need to carefully focus and listen.
29:03
Speaker A
of times that number is got repeated how many duplicates are there in this particular array so we need to maintain this count variable this is exactly same as almost searching an element but in the searching element as soon as the element is got matched immediately we
29:17
Speaker A
So first thing what we need to do is...
29:32
Speaker A
exiting from the follow finally we will check how many values we have in the count variable that is a logic which we have to implement okay now let's go to Eclipse take another class this is almost equal to search an element but slight change we have to do okay so number of repetion
30:04
Speaker A
or else we can simply say find how many duplicates in arring okay this is more clear how many find how many duplicates in a r okay so now the first step we have to create one array which should contains some data okay so
30:28
Speaker A
let me create one array here I can say int a equal to uh I can say some numbers let's say 100 200 again 100 300 again 100 4 100 again 100 okay this is my array now 100 is duplicated
30:49
Speaker A
three times okay so now we'll find how many times 100 is got repeated how many times 100 is is got repeated so let me first write the F Loop so I can directly write enhanced F Loop you can try
31:04
Speaker A
normal for Loop okay so I'm taking a I'm reading each and every value into variable in some value some variable then if I print this value it will print all the values from array so it will print all the values from array which includes all the duplicates everything is got printed everything
31:25
Speaker A
is got printed okay now we need to find find how many times 100 is got repeated okay so that number we can take a separate integer number equal to 100 so now we need to find how many times this number
31:40
Speaker A
is repeated so now what we need to do is instead of printing this value we will just compare if this value okay equal to number okay then what we have to do we have to take One external variable
31:59
Speaker A
count variable initially let's make it as a zero this is an integer variable count is required so as soon as you found a value or as soon as you found an value in ARR immediately increase the count value by one count Plus+ increase the count value by one okay now how it is going to execute
32:21
Speaker A
first 100 will store in value and 100 equal 100 is true now what is the count value what is the count value one okay now again it will go up take another number 200 store into value now again
32:37
Speaker A
compare 200 equal to 100 not matched so again it will go up so whenever the match is happened then only count value will be increased otherwise no so it will go up again 100 again one more 100 we
32:50
Speaker A
got here so again comparison is true so initially count value one here now it becomes two because we found one more 100 now again it will go up and take another value 300 not matched now again one more 100 we got so condition matched so the count value will be increased by one so now it becomes
33:11
Speaker A
three so how many hundreds are there those many times this if condition becomes true and those many times this count value will be increased by one every time whenever you found that element it will increase by one so finally if you come out from this follow just try to print the count
33:31
Speaker A
variable that includes or that contains the total number of reputations how many times that number is got repeated so the count is representing right every time you found that number we increasing this so finally the count contains a total number of elements how many times that element is got
33:49
Speaker A
repeated so when I run this you will get exactly four times so there are 400s are there one 2 3 and four okay now let us take 200 now I want to find how many 200 are there let's make 200 now so how
34:09
Speaker A
many times the 200 is repeated how many times the 200 is got repeated two times here it is one and here it is two okay so this is how we can simply find how many duplicate numbers are present
34:26
Speaker A
in this AR okay so we can write we can Implement so many Logics guys this is never ending process there are so many things so why we have uh why have you used number variable instead of using value 100 okay duplicate values where here I have used number only value equal to number
34:54
Speaker A
I've taken this number variable okay so whichever number you want to find duplicates you can just put that number here and that number variable I'm using here I think you not properly see so value equal to number here I'm not hardcoded the value I have taken the number variable
35:13
Speaker A
here okay and you can directly put the number also no problem suppose if you have a specific to number suppose I want to find how many times 100 is got repeated you specific to number then you can directly write a number here no problem okay you can directly number
35:32
Speaker A
so you don't need this variable you don't need this number variable that okay then this becomes a static because this will work only for 100 okay later if you want to check another number how many times repeated again you have to change if condition
35:47
Speaker A
here so instead of doing this we can just create one variable number and that you can specify okay yes can we use the same logic when there are multiple different values yes just now I have shown you right how many times 100 is got repeated how many times 200 is got repeated and
36:14
Speaker A
remember all the values should be numbers because array is a homogeneous data not heterogeneous data so every uh value should be having same data type and even if you have a different types you can still verify it by creating object type of an array okay multiple different values also
36:35
Speaker A
we can have but in that case you have to take the object type of an array then you can find it right so this is one example how to check how many array variables exist how many array variables
36:54
Speaker A
exist in a variable in a program you guys can try so here how many array variables I have created a is one array variable so there is no specific Logic for that actually so the variables will be defined by whom who will create a variables who will create a variables the user right the
37:16
Speaker A
program will create a variables so the time of creating the variables itself he has to aware where we have to create an array variable where we have to create a normal variable so variables will be used for operations that's the actual intention okay the variables we will use for
37:34
Speaker A
operations and we no need to count how many variables are there how many array variables are there not like that but we can check suppose if there is a a variable that that variable is a normal variable or array variable that we can check but in a program how many array variables
37:52
Speaker A
are there how many normal variables are there we cannot check like that okay but just tell me what exact requirement so accordingly we can TR the different logic but this is not possible okay all right so we have seen search an element and now we will see sorting example three sorting
38:26
Speaker A
and this sorting is very big concept guys there are almost 10 to 15 algorithms are there for sorting we can do different ways the N number of algorithms are there but uh now we are not going to learn each and everything I will show you one simplest way of sorting an array
38:45
Speaker A
by using a built-in methods okay and if you still interested to learn all those sorting algorithms but programming level that is important but we we don't need need that much actually but the programmers developers they have to learn all algorithms concept time complexity all
39:05
Speaker A
those things okay but we are not going in that concept just we'll try to understand how what is sorting and how can we sort array elements in the simplest way it can be number array or it can be string array whatever it is we will able to sort and there are multiple ways are there
39:23
Speaker A
bubble soft merge sort linear sort insertion sort transition sort okay there are 10 to 15 algorithms are there so if you really want to learn all those algorithms I will share some links at the end of the session and you can go through and learn but I'm not recommending at
39:40
Speaker A
this point of time just first be familiar with the simple Concepts the basic concepts and once you familiar with all the concepts then you can go with a little bit advanced stuff okay but if you look at the Sorting techniques and everything you will be confused totally first of all you may
39:57
Speaker A
not be able to understand okay very complex so simple simplest way I'll show you how we can sort an elements in an array so let's take a new class sorting elements sorting elements in Array so you can practice hundred thousands of programs based on this topic guys so it's very
40:23
Speaker A
difficult to discuss each and every example I'll show some example if you know how to use an array that is more than one so how searching will work in two Dimension array so you guys can practice all those things okay it will also you can also search an element in two Dimension so that is also
40:39
Speaker A
possible you can read each and every value from two Dimension AR and you can check if condition you can keep if condition same thing actually no there is no much difference only thing is you need to write another for Loop two different for Loops you have to write so you already know
40:56
Speaker A
how to read the the value from two Dimension are right how to read values from two Dimension are yesterday we already done this see if we go back to the yesterday session two Dimension are so by using this for Loop or enhanced for Loop or normal for Loop you already know how to read
41:13
Speaker A
the element here we are printing that elements from two Dimension so instead of printing here you can just put if condition if this particular value equal to our expected value then element found and exit from this F Loop if you put break command inside this for Loop it will
41:28
Speaker A
automatically exit from Outer for Loop also so here instead of print statement you can just put one condition if condition if this number is matching with our expected number and immediately change the status tag equal to true and break the loop after come come out from the break for
41:46
Speaker A
Loop you can put another condition if the status value still Falls then element is not found same logic no change only thing is you need to write another for that's it you guys can try if you want okay it's very simple actually it's not much complicated all right now we'll see how can we
42:09
Speaker A
sort an elements yes sorting means yes ascending or descending order we can print the numbers in ascending or descending order right so first of all uh let us see how can we sort the elements it can be number or it can be string whatever it may be we can do it and but this is a simplest
42:28
Speaker A
way by using built-in methods I'm going to show you but if you want to sort in desing order in whichever way you want order but we need to write a different type of algorithms okay let's try this first and before sarting we need to take one array concept right we need to
42:44
Speaker A
store some elements in Array let's take one array and uh in this I'm going to store the numbers in random order not exactly in the sorted order random order then only we will know AR is sorted or not so I'm taking 100 600 then 200 500 and then 400 so now I'm taking these numbers in
43:04
Speaker A
the random order it is not ascending order this is not a descending order so randomly I've taken this number 200 let's take 400 here and 500 okay now I want to print this array in sorted order so before printing this array in a sorted order just print this array as it is first before sorting I
43:26
Speaker A
will print element after sorting also I will print the element so that we can see exact difference so if you want to print this array system print so how we can print before sorting I'm writing xcept before sorting so before sorting I'm just directly printing this array by using enhanced
43:44
Speaker A
for Loop we can just try to print so a is my array I'm taking this into value and after that I'm just trying to print value so this will print all the elements from array before sorting before sorting
44:01
Speaker A
means the original array so this is the order of elements these are the order of elements okay and suppose sometimes if you want to print all the numbers just for printing purpose you don't need to write a looping statement okay why we need to go with the looping statement because if you want
44:17
Speaker A
to read individual values one by one then you can write a looping statement so that we will get each and every value individually but I don't want to read individually I want to read all the array elements in one single statement and later I don't want to perform any task just for printing
44:36
Speaker A
purpose okay then you have one shortcut method instead of writing this F Loop what we can do is we can directly print all array elements using one method here that is system. pin and here I can say there is a predefined class called arrays arrays is one of the predefined class
45:02
Speaker A
arrays dot in this there is a method called two string okay two string and uh in this we have to pass name of the array and this is one method which is available in Java right instead of writing this for Loop if you want to print all the array elements you can use this method arrays
45:25
Speaker A
is a uppercase character arrays dot two string of a what is a here a is a name of the array the variable name of the array so this will print all the array elements in the form of set when I run
45:41
Speaker A
this Java application so you will get all the numbers like this EXA in Array format okay so this is a shortcut and uh it is just for printing because it is collectively printed all the values vales so again we if you want to perform task any operation individual numbers we cannot do
46:01
Speaker A
here because this is all one set right if you want to perform any operations in this then we have to write a looping statement and read individual values and then you can perform some operations so this is just for printing purpose we can use this statement Aras do two string of a
46:19
Speaker A
and we don't need F Lo okay simply you can remove this or else no problem so before sorting these are the numbers currently I have now we want to sort this array and to sort this array again we
46:35
Speaker A
have to use arrays class in this there is a method available called arrays do sort this is a method array dot sort in this we have to just pass array name so whichever array you want to start you have
46:52
Speaker A
to specify and one more thing whenever you're trying to access some external classes aray is a another class actually so we have to import one package import to java.util.arrays actually this is a package concept the coming sessions I will explain in detail so for now just understand
47:13
Speaker A
whenever you refer any external classes we have to import required package so this array class is present inside java.util package these are all predefined packages and classes in Java they already developed in Java so we are just trying to use it okay so this import statement
47:32
Speaker A
must be present whenever use this arrays class okay if you're not writing this manually it will automatically suggest you so if I look at here it is giving some error right when you put the cursor in arrays it will show you some import statement here import arrays java. just click on this link
47:49
Speaker A
it will automatically import like this okay so this particular statement will sort elements sort elements in Array so we already printed these array elements before sorting now here we have done sorting so once we have done this sorting then we will print the values again one more
48:11
Speaker A
time so I'm executing the same two statements one more time after sorting so here I will just say after sorting again array. two string of eight right so before starting and here we are doing sorting of elements and then we are printing after sorting let's try to execute run as Java
48:33
Speaker A
application yes now we can see this is a before sorting elements and these are the after sorting 100 200 four five and 600 okay so the same technique is applicable for Strings also we can also simply sort strings let me show you another example sorting strings we have to
49:01
Speaker A
create array which contains strings multiple strings sorting strings take main method say finish okay so now let us take can we use arrays. two string method to printing string array yes you can print whatever array you want by using arrays. two string method let me try to use now
49:28
Speaker A
I going to show you that okay so let's create one string array string as equal to you can take any type of sing s equal to and here I will take some type of string data so here this method
49:43
Speaker A
is not allowing a decreasing order that is your descending order and if you want to print the descending order you have to write a different type of logic okay this particular method sort method is not allowed to uh print numbers in the descending order okay uh I I'll try to find out
50:03
Speaker A
maybe for Strings it is possible or maybe the numbers it is possible so let me cross check okay otherwise we have to write a logic separate Logic for that okay so let us see how can we sort the string so string is bracket equal to let me take some strings here see if it is a number we
50:24
Speaker A
can sort but how can we sort strings and how we will know which string is higher which string is lower if it is a string how can we sort see if I give a b c is one string okay XY Z one if I
50:42
Speaker A
ask you which one is the higher which one is the lowest s how we will tell if it is a number we can simply say lowest number highest number but if it is a string how can we do this yes so how
50:58
Speaker A
it will check whether which one is greater or not first it will check the first character here it is a here it is X alphabetical order okay it will check alphabetical order so here first character is what a here is first character is X and which one is highest and which one
51:14
Speaker A
is lowest a is the lowest so obviously ABC is the largest suppose if the first character is same first character is same then it will go to and compare the second character here it is a b here it is Y which one is the largest Y is the largest largest string okay ABC is not largest
51:35
Speaker A
okay like this first it will check alphabetical first character is equal or not if it is equal then second character if it is equal then third character so it basically checks the uh characters alphabetical order okay let's try to take this numbers so first of all let's take a
51:55
Speaker A
BC and single characters and then we will try to apply uh strings let me take something called a c b okay and here I can say d d c b a okay let's say take Single Character we will check whether
52:17
Speaker A
it is sorting in alphabetical order or not okay so now Tak an string and is it correct notation can we put characters in The Double quotes yes or no can we put characters in The Double quotes yes okay yes and uh can we put this character because they are single characters can we put
52:40
Speaker A
in the single PS like this can we put single course because if they are the characters right yes we can put but the type of this array should be what G type of an array okay if you take care type of an array we can simply create so let us try this first let us
53:00
Speaker A
sort this array so again I'm just trying to print before sorting before sorting I'm just trying to print how to print before sorting okay I can directly put like this in writing another statement here itself before sorting concatenation and which class we have to use
53:20
Speaker A
arras do two Stringer arras do two string of what name of the array is what yes okay that is a before sorting now I'm going to sort arrays dot do sort of yes so this command will sort the
53:37
Speaker A
elements now after that again I'm printing after sting after sting okay same thing let's try to execute okay now we can see it is sorted in descending order a b c d so we have successfully sorted character array let us try to sort string type of an array okay
54:07
Speaker A
so let me take a string yes equal to the bracket first string I'm taking squ second string second string Mar and the third Stringer John then fourth Stringer David so let's take some random string like this this is my array four elements are there now let us see before sorting after
54:42
Speaker A
sorting yeah so you can see this is the before sorting and this is the after sorting so first D comes into picture right so D first character is D first lowest so D next J and then M next Scot
54:58
Speaker A
yes so even first cars itself it is matched in alphab Bas alphabetical order it is got sorted so this is how we can simply do the sorting by using buil-in methods okay sorting of you can do numbers sorting you can do characters you can do strings of sorting any type of data this
55:20
Speaker A
method works okay so this method Works erase. two string method Works to print arrays any type of array you can print and the sort method is applicable for all kinds of arrays it can be string type of data or character buole whatever it is you will able to sort it okay so this is one
55:43
Speaker A
example now uh the next example example four so print array elements print array elements in reverse order I just want to print array elements in the reverse order so reverse order is nothing but in whichever order we have we have to just reverse order we have to print that means
56:18
Speaker A
reversing an array we can just simply reverse an array okay so first let us think about the logic how to print elements in the reverse order how to print an elements in reverse order let's say first I'll take one array like this and I'll store some values let's say 1 2 3 4 order is not
56:40
Speaker A
important you can put any number of orders whichever order you want random numbers no insues now I want to if I print this array all the values one by one how we will print first it will print one then two three four five by using for Loop we will read one by one like
56:55
Speaker A
this but now I want to print at the from the bottom in the reverse order okay so how can we print we have to start from here and then we have to decrease every time in every itation till we go to zero element right so this is the logic we have to uh apply okay now we'll try to
57:17
Speaker A
implement okay reverse an array so an array we have to reverse reverse in the sense we will print all the elements in the reverse order okay now first we have to take one array variable again it can be string array character array number array whatever you want or it can
57:45
Speaker A
be object array no problem okay so let me take one integer in a so here I'm taking 2 3 4 five six okay let's take five you can take anything you can change these numbers later so this is
58:00
Speaker A
my array now I want to print this numbers in the descending order or you can take another numbers 100 200 because 1 2 3 4 we will represent with the index number right so let's take a different numbers 300 400 and then 500 okay now if you want to print these numbers
58:20
Speaker A
in the reverse order and arase do two string method will display these numbers in as it is same order it cannot reverse now we have to write a logic for this to print this numbers in the reverse order we have to use classic for Loop enhanced for Loops won't work okay because
58:43
Speaker A
enhanced for Loop will read the values one by one from the beginning itself okay enhanced for won't work here so we have to read the values from the end so only through index it is possible right so we have to write one F Loop like this for now tell me what is an index starting
59:03
Speaker A
index see this is my array and in this we have these numbers 1 2 3 1 2 3 4 5 so here we have 100 200 300 400 500 this is 0 1 2 three and four now we have to read from here B to bottom to top
59:30
Speaker A
now tell me what is I value starting from so here we have to start I value equal to what equal to what so normally we will start from zero right if you want to read the data from top to bottom now
59:45
Speaker A
we have to read the data from bottom to top now what will be the I value here what will be the I value starting point is this one what will be the I value how we will know the starting point
59:55
Speaker A
first of all okay we don't know starting point so dynamically we have to get so if you want to know this index is a starting point how we will know this dynamically a do length minus one a do length will give you four a do length will give you five 5 - one four that is exactly equal to the
60:16
Speaker A
last index so we have to start from here right so then here the I value should be what a DOT length minus one that is a initial I value then what is the condition what is this condition condition I'm
60:35
Speaker A
talking so when we have to stop reputation so anyway I value will decrease okay I value here index will decrease minus minus I we will do but when we will stop as soon as I value becomes zero as soon as I value becomes zero we will stop or we can write multiple condition I value zero then
60:58
Speaker A
we can stop so even I value zero also we should get right so we should not say equal to I should be greater than what is the condition I should be always greater than zero only because here these
61:14
Speaker A
are all indexes 4 3 2 1 all are greater than zero or not yes I greater than Z even equal to also we should put so I greater than or equal to Z this is a condition equal to also zero also we should
61:28
Speaker A
get so the condition is what I greater than or equal to Z then plus plus or minus minus minus minus because we have to reduce this value minus minus minus okay so int data type also we have to specify here int like this this is how we can write the conditions now just print
61:54
Speaker A
the I value here how to print I value a of I values we want to print so a of I right so then execute okay now we can see these are the initial values 100 3 4 5 and now we got a descending
62:15
Speaker A
order right so this is how we can print array elements in the reverse order okay now these are few examples and some of them I will give you as an assignment you guys can try and uh before that how to read data into array at runtime okay so how can we take the input from the keyboard so
62:42
Speaker A
as of now what we are doing if you want to create an array we are just creating and we are directly assigning the data now my requirement is I want to provide this data in runtime that means once
62:55
Speaker A
you run your program or once you executed your program it should ask enter this number enter the position then that number should go and add into that particular position in the array okay so now we'll see how can we read the input from the keyboard or how to read the input from the
63:13
Speaker A
console and runtime and once you understand that then we will see how to read the data into array how can we store the data into array in runtime because this is most uh popular use case and very important because many numbers dynamically we should provide at the run time instead of
63:32
Speaker A
hardcoding this data okay now let us see how to take input from the keyboard at the run time how to read the data in runtime first we will check we will see the small numbers and then we will come
63:47
Speaker A
to the array part again so here taking input from keyboard keyboard in the sense from the console okay in the console normally we are able to see the result or output we are able to see in the console window so now we are able to provide the input also we can also provide the input from the
64:07
Speaker A
console that is your keyboard taking input from the keyboard take this main method and say finish and different data type also we can accept I'll tell you how to take a different types because depends on the data type you have to change the methods right so normally what you will do is you
64:25
Speaker A
can say suppose integer number equal to 20 I'm taking and immediately I'm trying to I'm trying to print this number is equal to 20 so this is hardcoded value sorry it is number so what is this number number is a hardcoded value we already hardcoded this value here and we are just printing
64:44
Speaker A
but instead of hardcoding this value here I just want to take this number from the console window so that I can provide different numbers and you can test with the different numbers okay so to do this what we can do is instead of hardcoded this value this is called hardcoded value instead of
65:02
Speaker A
doing this I want to take this number from the console window at the runtime okay so for that we have a special class is available in Java which is also buil-in class which is scanner class okay scanner so whenever you want to use any class we have to create one object for this okay
65:24
Speaker A
at this point you won't understand what is class and object all those things coming sessions you will understand but for now just follow blindly whatever I'm saying scanner equal to new scanner and inside this we have to pass one parameter that is called system.in because we are taking
65:42
Speaker A
input from the system so we have to pass input so this is a statement so whenever you are using any external class we have to import that particular class how to import when you place the cursor on it it will show you import scanner of java.util just click on this link so it is got imported
66:03
Speaker A
java.util scanner got inputed now this error is gone so this is a first statement so whenever you want to take any input from the keyboard or console you have to create scanner class object so this is how we can create scanner class object scanner this variable is can be anything you can
66:20
Speaker A
put a BC or XY Z whatever this is just a variable name so scanner equal to new scanner this is the first statement Now by using the scanner object we can take the input from the keyboard so how can we
66:33
Speaker A
take SCA dot so what type of data you want to take that we have to be clear first integer type of data I want to capture so SE dot next in this is the method you have to use if you want to accept
66:51
Speaker A
integer data from the keyboard or from the console you have to use next int and uh this will ask you to provide the value this will expect the value from keyboard or console once you provided the value that I'm going to store in a variable like this okay so this command will take the value from
67:17
Speaker A
the console that I'm storing into this variable and then again I'm printing this value so this is how we can do it so first scanner class object we have to create like this scanner is equal to new scanner system do in is a parameter by using this scanner object we can call one method called Next
67:35
Speaker A
in so this will accept a numeric data only without decimals and that I can store in the number and that I'm directly printing or you can perform any type of operations so let me try to run this and
67:49
Speaker A
just observe what will happen yeah so actually the cursor is waiting in the console window it is still in running mode you can see in the red color Mark here it is running but do you know exactly what it is doing it is not asking for any input actually but it is waiting for input
68:08
Speaker A
we don't know whether it is waiting for water or not but it is waiting for input so if you provide some input like this I'm passing 100 here in the console window I'm typing 100 when you press enter
68:19
Speaker A
now we can see we got 100 as an output so this 100 whatever you Pro Ed here that is taken by this sc.
68:26
Speaker A
next in put that number in this number variable then this command is got printed same number value okay but if you look at here when you run this program do you know exactly it is waiting for the number or not you don't know right so what we can do is simply you can write one simple message
68:46
Speaker A
here so before taking this number you can just write one print statement so that you as a user we will know you can say enter a number enter a number okay so now when I run this it will print
69:03
Speaker A
as it is in the console window now we can see enter a number so whatever is put in the double quotes exactly printed now we will know exactly it is asking for the number so put the cursor here
69:14
Speaker A
and then provide the value like this and press enter now we got the number okay so this is how we can take a number input from the console window by using next in this is not mandatory statement but in the runtime it will be more clear because as a user we have to know whether we have to pass
69:39
Speaker A
a number or string or character whatever so if you write this message clearly you will exactly know okay and also at the time of print we can make more sensitive I can say given number is and then I'm printting this number it will be more clear now it is asking enter a number I'm saying 50
70:02
Speaker A
now given number is 50 so like this we can take input from the console window at runtime and we can store into some variable and then we can print this number as it is so if it is a number we have
70:17
Speaker A
to do like this now let me take some other type of data this is for number okay now how to take decimal number if it is a take decimal number same scanner class this we won't change okay so decimal
70:33
Speaker A
number how can we take so let me put one more system. print and I'm asking user to enter decimal number enter a decimal number now how to take the decimal number SE dot here we use next in now this
70:50
Speaker A
time here we have to use SE do next Double next Double so this will take the decimal number data and we have to store in a variable now what is the type of this number variable now what's the data
71:04
Speaker A
type of this obviously it should be double type right because we are accepting double type of data and we are storing into a number that variable also should be double type of variable right now I can just print this given value s given value s or can just print number okay let's try to
71:35
Speaker A
execute so now this time it is asking for decimal number so I'm just providing some decimal number let's say 10.5 decimal number given value is 10.5 so decimal number is asked okay for example even though it is asking for decimal I'm just passing an integer value will it work or not can you guess
72:03
Speaker A
will it work or not I'm just giving only integer instead of double I'm just providing an integer will it work or not 10.0 okay because we have taken that as a double so it is accepted but it is additionally added do0 so same only right so 10 10.0 no difference it is just added to a
72:25
Speaker A
decimal point zero that's it so because both are number type of data so it is accepted no problem okay now we'll see the vice versa let's say I'm taking integer here in the first example let us enable this okay now here we have to pass an integer actually right as for next we have to
72:50
Speaker A
pass integer but now I'm passing a decimal number passing decimal number will it accept or not will it accept or not exception input mismatch exception and what we need to understand based on this integer data type variables cannot hold decimal numbers whereas decimal data type
73:17
Speaker A
variables can hold integer values okay that is a point so now we have seen how to take integers and how to take decimals but how to take string how to take a string so let's see this because string is little bit different okay so let me just say system P enter your city okay
73:47
Speaker A
so just I'm giving some message enter your city so normally city is what string only right that I'm I'm going to store in a variable so now what is the type of the variable it should be string type of variable right string City equal to Sea Dot now here we used next in here it is a next
74:07
Speaker A
Double but here what is a method we have to use we do not have next string we don't have a next string just we have to call only next that's it just next so this will take the string and
74:23
Speaker A
store into city city is a string variable now I'm going to print here system print I can just print city as and close and just print City so this is how we can take string type of data for all
74:44
Speaker A
other data types there are methods are there like string next in for integer next Double is for decimal next float is also there next Bull is also there next car is also there okay but for Strings we have only this one SC do next same scanner object we have to use to access these
75:03
Speaker A
methods okay so can we have one single scanner class for all yes we can have no problem same object we can use for all kinds of data types okay but the variables should be different because here we are taking integer the variable should be integer type here we are taking decimal the
75:24
Speaker A
should be double type here we're taking string the variable should be string type same variable we cannot use for multiple data types okay so now execute so it is asking for enter a city okay now I'm entering some City Delhi let's say your city is Delhi so like this we can provide the input
75:48
Speaker A
from the keyboard or console and we can see output in the console itself console is for not only for providing output it is also used for providing input and by using scanner class is this clear everyone how to take input from the keyboard now we will apply the same concept for arrays can we
76:12
Speaker A
enter multiple yes that's I'm coming to that part yes we can also provide multiple inputs yes it is possible I will show you in the next example so before that is it clear first of all how to create a scanner class object and how to use scanner class object to take the input
76:31
Speaker A
from the console integers decimals strings anything you can take okay now let us try to take multiple values how to take multiple values from the console window let's take another class taking multiple inputs from keyboard or console taking multiple inputs from keyboard or console
77:01
Speaker A
take this main method okay now process is exactly the same okay now what I will do is this time I will take two inputs from the console or keyboard then I will perform some operation on those two values so now tell me what is the first step what is the first step we have
77:22
Speaker A
to create scanner class object that's the first step okay scanner is equal to new scanner inside the scanner we have to pass one parameter system. in so whenever you write this we have to import this scanner class from java.util package done and sometimes if this popup is not coming like
77:44
Speaker A
Auto s is not coming you can directly manually write this statement okay sometimes you may not get this Auto because we need to do some setting in Eclipse okay if there is any problem but that is not a blocker actually okay if not able to see the suggestions like this you can try
78:03
Speaker A
to write this manually no problem so now let us take some values from the keyboard now the first value I'm taking from the console system print I'm asking enter first number first number I'm asking first number to enter so how to take this first number SE dot next in and that I'm going to
78:25
Speaker A
store in one variable int number one first number accepted now I'll ask another number system print enter enter second number enter second number and that I'm going to take another same object SE do next enter same type of data that I'm going to store in another variable int number two
78:55
Speaker A
okay into object means you can provide any dep no issues so here uh okay let me come to that point first let me finish this okay so number one and number two so we have taken two numbers now so here we are taken number one here we have taken number two so now we want to perform some
79:13
Speaker A
operation let's try to add those numbers and say number one plus number two this is addition of two numbers and gation and put this expression in Brackets okay right so this is how we can take multiple input so this is first input
79:41
Speaker A
I'm taking second input and then perform the operation okay let us try this run as Java application yeah enter first number 100 second number 200 now addition of two numbers is 300 so you can also pass multiple numbers like this okay suppose if you want
80:04
Speaker A
to pass different data types like 100 next one is a name some string country address phone number okay different type of data if you want to pass then you have to use a different type of methods okay so for example let me take another example this is example one
80:26
Speaker A
okay so now the first input enter name enter name okay name is nothing but what string right so SE do next this will take the string and store in the string variable string name equal to next and then you can print so your
80:55
Speaker A
name is can just print name so this is how we can take the name so now what is the next value let's take a number so suppose four number so let's take four number then SE DOT phone number phone number
81:13
Speaker A
is what yeah or age you can take age SE dot next in age is also number right so this time we have to take next in so here age is a number so int age now we can just print this okay your age is can
81:32
Speaker A
just print age but before that we have to print one more statement so here enter your age like this so name we have taken which is string age we have taken which is number okay so like this we
81:50
Speaker A
can accept any type of data so depends upon the number number but this is object in the sense object is a totally different type okay suppose if it is a variable object then how can we take for example let's say uh system do out. println enter uh unknown type suppose unknown value unknown
82:18
Speaker A
value means it can be integer it can be double or it can be string whatever it is unknown value value so that unknown value have to take into variable some variable you have to take so we don't know what is the type of unknown value right it can be integer it can be number whatever it
82:32
Speaker A
is we don't know so let's take an object variable object okay and say value equal to s dot now this is a challenging thing so which method we have to use because we are providing some unknown value we don't know whether it is integer or string or decimal we don't know exactly right so in those
82:54
Speaker A
case also we have to use just next method because this next method will take anything as a string format anything as a string format it can even if you provide the number decimal character string whatever everything will be converted into string format and the object variable right this variable
83:15
Speaker A
can hold any type of data but later if you want to perform any operations on the numbers we have to convert the string to number again because we cannot perform arithmetic operations on the string data types so we have to convert the string format to number format and then we will able to
83:34
Speaker A
perform the operations okay so it can take like this and then uh you can print this system. print LM you can just print value okay just observe here okay now I'm executed so so enter a name say John this is obviously string your name is John enter a age let's say 30 your age is 30 now
84:02
Speaker A
enter unknown value so unknown value means it can be anything even though we use next method that will accept all kinds of data but everything should be in string format so even if you provide 100 I'm providing will it take or not here I'm using next normally it is for string but here
84:21
Speaker A
we are providing 100 will it take or not will it take or not yes it will take okay it will take 100 and how in which format it will take because we use next even though we provide the number it
84:38
Speaker A
will take that number as a string actually 100 is a string not a number okay so this is how we can handle object once you once you get this number as a string later we have to convert that into
84:56
Speaker A
again number format then only we can perform the operation okay so that is again different process what if I want to accept care or bullion yes you have methods next car is method is there so you can see here SE dot next care sorry dot I think for next car is not there but for that
85:20
Speaker A
you can use again string method next method but rest of the rest of the data types you have all the methods you can see next Boolean is there okay next next to double next float next int so for every type we have methods next long next short for all primitive data types these
85:40
Speaker A
methods are available so you have to just change this method according to your value if it is a Boolean we have a next Boolean also you can see this is the next Boolean so you can also accept Boolean data so just you need to change this method okay so for almost every data type there
85:58
Speaker A
is a specific method as there and if there is no specific method you can use this next method for all kinds of data types you can if you use next you can provide string you can provide number you can provide character you can provide P all kinds of data you can pass okay so finally we
86:18
Speaker A
will apply the same concept for arrays so now my requirement is I will create one empty array then I will fill the data into array by taking input from the user at the run time I will take the input and store the data in Array and after that I will print array elements the last
86:37
Speaker A
example okay so here the name is reading and writing data into array reading and writing data into array okay now earlier if you want to add some data into array we have two approaches right so what are those two approaches the first approach is what
87:12
Speaker A
int a equal to new int here we specify size of an array and then we can add some data like a of zer 100 and then a of two a of 1 is equal to 100 so like this we have added this is one
87:28
Speaker A
notation right so now this time I and another way is what I can directly write this a bracket equal to in the cly braces we directly specify the values like this this is another approach we have followed now my requirement is I will just create an array without any content just
87:47
Speaker A
create one empty array and five inputs I can store but those five inputs should come from the keyboard or should come from the console in runtime okay so I will take five different values and I will store those five different values in this five positions that is called
88:11
Speaker A
reading data into array and once you added all the data into array then I will write the same data in the console window that's my objective okay so let's start with this first what we need to do we need to take a scanner class scanner ESC equal to new scanner
88:30
Speaker A
system.in system. in we have to import this scanner class okay now so this time we have to take multiple inputs see very simple if you want to take five input from the user if you look at my previous example different statements we have to write so this is for one input this
88:55
Speaker A
is for second input so in Array we have to store five elements so we have to write five times the same statement you have to repeat five times right enter first number second number third number fourth number and if you don't know the size of an array we don't know how many times we
89:11
Speaker A
have to ask input from the user right so instead of writing this next in methods multiple times we just write one for Loop and repeat that multiple times okay so let me just say for I'm starting array index start from what zero only right I equal to zero and how many times it should
89:34
Speaker A
repeat depends upon the size of an array so I less than I less than a DOT length and every time I'll increase I value index value will be increased by one so now we need to ask the numbers
89:50
Speaker A
from the keyboard or user so first value second value third value and so on so here I'm asking system.out.print enter enter a number enter a number or you can say enter a value and once you take how to take the value from the keyboard SE dot next in next int so that I'm
90:19
Speaker A
going to store and where you have to store this value okay we are taking input from the keyboard from the user where we have to store in Array we have to store again in Array where we have to store because we have a multiple indexes so in Array we have a five locations in which
90:36
Speaker A
location we want to store suppose this is my array 0 1 2 3 4 five so now first value are entering in which position we want to store in the first position next round of iteration we will take another number where you have to store second position then third position four
90:53
Speaker A
like this we have to store so how to decide in which position we want to store the value a of I just simply pass I value here okay so that a i representing the index location so the first value will store in the first position okay and next round it will iterate again it
91:16
Speaker A
will ask another value in another position next round again it will ask another value in another position so once this Loop is completed all the data will be stored in Array okay then we will try to print this array let's try to print system do printing printing array elements so how
91:42
Speaker A
to print this array elements same thing again same for Loop or you can also use array arrays do two string method directly if you don't want to use a looping statement you can simply say printing array elements arrays dot two string of a which is array name okay now let us try to
92:11
Speaker A
execute yeah enter a value 10 enter value 20 enter value 30 enter value 40 Enter value 15 done so we have added all the numbers and dis printed array and if you look at this output every time it is asking just enter a value but we don't know in which position that value is
92:35
Speaker A
going to store right in which position it is asking for we don't know so we can write more meaningful message enter a value enter a value or which value in which location enter a value for the position for the position which position I I representing the position right position of I
93:01
Speaker A
concatenation like this okay and then put column like this so that we will be more clear in which position it is asking for the element enter value for the position okay now execute it will be more meaningful observe now we can see enter the value for the position zero 100 and passing now enter
93:22
Speaker A
the value for the position one 200 enter the value for position two 200 enter the value for position three 400 enter the value for position four 500 okay so it will be more meaningful so now we are able to store data in Array and also we are able to read data from array so dynamically we can pass
93:47
Speaker A
data into array by simply writing one for Loop statement okay so these are the some examples you guys can practice each and every example and which is most important you will be asking so many type of questions from aray concept especially there are so many programs you have to practice still
94:07
Speaker A
more because it is not possible to discuss each and every program 500 programs you can practice based on this concept some of them I will give you assignment but after completion of java sessions I will also give you some list of programs and uh you can refer them so this is all about Aras
94:25
Speaker A
concept and two dimensional is not that much important you can just try to understand how to read data and how to write data into the two Dimension array that is more than enough for now and please concentrate more on single dimensional and different type of operations okay now based on
94:44
Speaker A
this I will give you some assignments you guys can try this example five we already taken reading data into array by using scanner class we will able to achieve this okay now assignments So based upon today's topic only I'm
95:05
Speaker A
just giving this assignments and you can try this only two assignments and tomorrow I will discuss about strings concept and then I will also give some more assignments related to string okay first uh sorting elements using looping we have seen
95:28
Speaker A
sorting by using build-in methods Aras do sort method okay and there are so many methods are there as I already told you there are different type of algorithms are there and uh you guys can go through these two videos and try to understand but not mandatory now in future you can try this
95:49
Speaker A
okay and uh second assignment find missing number in Array this is the most popular example find missing number in Array suppose in Array I provided some data like this 1 14 52 or 1 1453 right which number is missing in this which number is missing 1453 there is one number is
96:12
Speaker A
missing in this two is missing okay that we need to find out but what is the prerequisite numbers should be in range numbers should be in the range what order is not important it is started from one two is missing but three is there four is there five is there so one to five is a range
96:32
Speaker A
actually and only two is missing so the first prerequisite is what numbers should be in range when you create some data you have to provide the numbers in particular range and should not have a duplicates that's another prerequisite okay this should not have a duplicates so you can easily
96:50
Speaker A
find the missing number there is small logic is there so if you apply that logic you can easily find out so try to do this I I'm providing this reference very interesting example also if you look at this video if you watch the video you will understand very easy and very interesting
97:07
Speaker A
also I can just find this reference video and you guys understand you can try yourself find missing number in in area okay and similarly there are some more programs are there find largest number in Array and similarly find lowest number in ARR largest number and lowest number I
97:36
Speaker A
will provide the links later here and you guys can try this find the largest number in a find the lowest number in a so these are the four assignments you guys can try and I will provide the links to these also and after providing the links I will upload okay fine so this is all
97:57
Speaker A
about array concept and tomorrow session we will discuss about strings which is also most important popular topic and aray concept still continues so many places many cases we use this arrays concept so you will understand so many examples also in the coming sessions this is just uh the
98:16
Speaker A
beginning okay so in the coming session you will try to use these Concepts in many places in many scenarios even in automation also we will use this concept in many use cases so very important topic you can't skip it every example you have to practice till you familiar with this
98:34
Speaker A
concept okay so that's all for today's session I'll stop here and we will continue tomorrow
Topics:Java arrayssearching algorithmssorting algorithmslinear searchbubble sortinsertion sortmerge sortcoding interviewSeleniumSDET QA

Frequently Asked Questions

What is the simplest way to search for an element in a Java array?

The simplest way is using the linear search algorithm, which checks each element sequentially until the target is found or the array ends.

Why is it important to understand the logic before coding array problems?

Understanding the logic helps in framing the problem correctly, making the coding process easier and reducing errors.

What should you do once the searched element is found in the array?

Once the element is found, you should break the loop immediately to avoid unnecessary comparisons and improve efficiency.

Get More with the Söz AI App

Transcribe recordings, audio files, and YouTube videos — with AI summaries, speaker detection, and unlimited transcriptions.

Or transcribe another YouTube video here →