课程: Java: Advanced Concepts for High-Performance Development

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Generic methods in Java

Generic methods in Java

- [Instructor] Generic methods are a special type of method that let us pass in different types of objects as arguments. This is best shown with an example. So in this example, I have a class called GenericMethods and inside the class I have a main method and inside the main method I have two arrays. The first one is an array of strings and that contains the words apple, banana, and pear. And then the second one is an array of integers and that contains the numbers 1, 5, and 7. So let's say I want to write a method that lets you pass in either of these arrays and converts that array to a list. I want my method to work with arrays containing any types of objects. So I could write two separate methods here. One for converting arrays of strings to lists of strings and another one for converting a array of integers to lists of integers. But using generics, I only need to write one method. So the first thing I need to do is…

内容