10.1 Générique introduction
Hello welcome to this new course on generics in the previous course we saw how we could introduce parameters of any type in a class in this course we will see how to do the same for the methods we call them generic methods I'll start by showing you an example of a generic method with only one parameter we'll suppose that we want to have a statice method that allows to draw randomly element of an array provided in argument so of any type so to begin we will create a method we create our class first HassardMethode so to create a method of generic type we simply have to do we for examples we will square a method statice statice so now to say to Java that we have to make a method of generic type we have to put inferior superior and inside it is there that we will indicate the type of genericity be the or the part next we will see that we can put two parameters therefore we will put T and here of course you could put all that you wanted we can put type for example or other now we will dare the question that is going to send us our method it is going to send us a type T and we are going to name our method random now in the parameter we are going to say our method that sends us an array of We will make variable Int n equals there before the size of our array then we will make a condition if n the size of the array equals 0 we will return a null now I will show you a concept that we will see in the rest of the course we will just make a brief introduction we will make Int of i equals between parenthesis Int between parenthesis n times Math. ramdom here we have just performed a cast is the fact of forcing the compiler to consider a variable as being of a type that is not the declared type or the real type of the variable that means that here our n times Math. Radom will return a double number so a number with a decimal point and the class math contains a series of method of mathematical variable here we are going to use the method Radom which will allow me to return an unspecified number so here we are going to carry out a return value of i I hope that you had understood this method we have just carried out just to specify here you could be to put public or to leave it that will not change anything now we are going to go in the method main to be able to test our method so to test it we are going to create an array Integer tab01 which is going to be equal to an array which contains values then we are going to display array and then we are going to concatenate the display of our array by our method chance so we call our method chance and in the parameter we are going in argument tab01 remember that a generic type must always be a class it is the reason for which we put Integer so to declare an array we can put Int and that will make the deal except that being that we want a generic type we have to put a class and the class of Int is Integer and we are also going to make an array of String tab02 forgive Hello for the first element Hi for the second then to finish Hello we are also going to display our array 02 random and we are going to call our method Random which is also going to take as argument our String tab02 String being a class we can directly put it as argument now we are going to compile our program and see if our method works correctly as you see it our method works correctly it could take in argument a string of character more an integer we are going to recompile the program and to see if our algorithm here will work also this one works correctly the first number being 2 Hello we are going to recompile and that is going to change 32 Hello I hope that you understand this brief introduction on the generic method we are going to carry out a challenge on the generic method which takes in parameter two arguments therefore See you soon