Sort by

recency

|

274 Discussions

|

  • + 0 comments

    public static void main(String[] args) { Object[] arr = {1, 2, 3, "Hello", "World"}; printArray(arr); } static void printArray(T[] array){ for(T element: array){ System.out.println(element); } }

  • + 0 comments

    static void printArray(T[] arr){

    for(T nums:arr){
    
    System.out.println(nums);
    
    }
    
    }
    
  • + 0 comments

    import java.io.IOException; import java.lang.reflect.Method;

    class Printer { public static void printArray(T array[]){ for(T temp : array) { System.out.println(temp); } } }

    public class Solution {

    public static void main( String args[] ) {
        Printer myPrinter = new Printer();
        Integer[] intArray = { 1, 2, 3 };
        String[] stringArray = {"Hello", "World"};
        myPrinter.printArray(intArray);
        myPrinter.printArray(stringArray);
        int count = 0;
    
        for (Method method : Printer.class.getDeclaredMethods()) {
            String name = method.getName();
    
            if(name.equals("printArray"))
                count++;
        }
    
        if(count > 1)System.out.println("Method overloading is not allowed!");
    
    }
    

    }

  • + 0 comments
    class Printer
    {
       public <T> void printArray(T[] array) {
           for (T element : array) {
               System.out.println(element);
           }       
       }
    }
    
  • + 0 comments

    //This is my solution, use in case to need

    public static void main(String[] args) {
        List<Object> result = Arrays.asList(1, 2, 3, "Hello","World");
        for (Object object : result) {
            if(object instanceof String){
                System.out.println(object.toString());
            }else{
                Integer temp =(Integer) object;
                System.out.println(temp);
            } 
        }
    }