Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,166 questions

40,722 answers

573 users

How to use generic method in Java

1 Answer

0 votes
public class MyClass {
    public static <T> void printArray(T[] arr) {
        for(T element : arr) {
             System.out.printf("%s ", element);
        }
        System.out.println();
    }
    public static void main(String args[]) {
        Integer[] intArray = { 4, 8, 1, 3, 9, 4 };
        Double[] doubleArray = { 3.14, 2.15, 8.43, 9.19 };
        Character[] charArray = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
     
        printArray(intArray);   
     
        printArray(doubleArray);   
     
        printArray(charArray);  
    }
}
    
    
    
    
/*
run:
    
4 8 1 3 9 4 
3.14 2.15 8.43 9.19 
a b c d e f g 
 
*/

 





answered Apr 19, 2020 by avibootz
edited Nov 14, 2023 by avibootz

Related questions

4 answers 36 views
1 answer 49 views
1 answer 57 views
57 views asked Dec 19, 2020 by avibootz
1 answer 77 views
77 views asked Jan 8, 2016 by avibootz
...