Question

JAVA Write a static method that takes an array of a generic type as its only...

JAVA

Write a static method that takes an array of a generic type as its only argument.

The method should display the array and return the number of elements in the array.

Test the method in main with at least three arrays of objects.

SAMPLE OUTPUT

Here is an Integer array 12 21 7 16 8 13

That array held 6 elements

Here is a String array

one two three four

That array held 4 elements

Here is a Double array

1.1 2.2 3.3 4.4 5.5

That array held 5 elements

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Java code

public class GenericArrayDemo {
    public static void main(String[] args)
    {
        Integer arr[] = {12,21,7,16,8,13};
        System.out.print("Here is an Integer array ");
        int size;
        size = displayArray(arr);
        System.out.println("That array held "+size+" elements");
        //==================================
        String arr1[] = {"one","two","three","four"};
        System.out.print("Here is an String array ");
        size = displayArray(arr1);
        System.out.println("That array held "+size+" elements");
        //=====================================
        Double arr2[] = {1.1,2.2,3.3,4.4,5.5};
        System.out.print("Here is an String array ");
        size = displayArray(arr2);
        System.out.println("That array held "+size+" elements");
    }

    /**
     *  takes an array of a generic type as its only argument.
     * @param array
     * @param <T>
     * @return
     */
    private static<T> int displayArray(T[] array )
    {

        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+" ");
        }
        System.out.println();
        return array.length;
    }
}

//Output

//If you need any help regarding this solution ............. please leave a comment .......... thanks

Add a comment
Know the answer?
Add Answer to:
JAVA Write a static method that takes an array of a generic type as its only...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The Language is Java GenericMethodTest (20) Download the OverloadedMethods.java program. Replace all the methods except main...

    The Language is Java GenericMethodTest (20) Download the OverloadedMethods.java program. Replace all the methods except main with a single generic method that produces the same output. Call the new file GenericMethodsTest.java. OverloadMethods.java: // Printing array elements using overloaded methods. public class OverloadedMethods { // method printArray to print Integer array public static void printArray(Integer[] inputArray) { // display array elements for (Integer element : inputArray) { System.out.printf("%s ", element); } System.out.printf("\n"); } // method printArray to print Double array public...

  • java Write a generic method that takes an ArrayList of objects (of a valid concrete object...

    java Write a generic method that takes an ArrayList of objects (of a valid concrete object type) and returns a new ArrayList containing no duplicate elements from the original ArrayList. The method signature is as follows: public static ArrayList UniqueObjects(ArrayList list) Test the method with at least 2 array lists of different concrete object types. Label your outputs properly and show the array lists content before and after calling method UniqueObjects

  • JAVA Question a) Write a generic method public static <...> Collection<Pair<...>> sortPairCollection(Collection <Pair<....>> col) in a...

    JAVA Question a) Write a generic method public static <...> Collection<Pair<...>> sortPairCollection(Collection <Pair<....>> col) in a Utils class that takes as parameter a collection of Pair<K,V> objects and returns a new collection object (use ArrayList<...>) with the pair elements from collection col sorted in ascending order. For comparing pairs, the K type must implement Comparable<....> Use the proper type constraints. b) Write a main() function in Utils.java that tests the sortPairCollection() with K=String and V=Integer.

  • Write a generic method gReplace() that takes a 2D array of generic elements and two scalars...

    Write a generic method gReplace() that takes a 2D array of generic elements and two scalars of the same type as the array elements. The function must replace all occurrences of the first scalar with the second one. (scalar: given int x, x is a scalar of type int) with java

  • Write a java program that has a method called sameArrayBackwards. The method takes an array of...

    Write a java program that has a method called sameArrayBackwards. The method takes an array of integers, and checks if the numbers in the array are the same going forward as going backwards and will return a boolean (true or false) depending on the result. You can assume that there is at least one element in the array. Method Header: public static boolean sameArrayBackwards (int [] arr) You will test your method in the main method of your program by...

  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

  • 11) Write a method (including header and body) that takes as an input an array of...

    11) Write a method (including header and body) that takes as an input an array of doubles and f three doubles containing the average, the maximum and the minimunm values of the input array. Test the program by calling the method from the main method. For example Input: 1 2 3 45 Output: 3 5 1 12) Write a Java method that takes a string as an argument and returns the reverse the string. Test the program by calling the...

  • package week_3; /** Write a method called countUppercase that takes a String array argument. You can...

    package week_3; /** Write a method called countUppercase that takes a String array argument. You can assume that every element in the array is a one-letter String, for example String[] test = { "a", "B", "c", "D", "e"}; This method will count the number of uppercase letters from the set A through Z in the array, and return that number. So for the example array above, your method will return 2. You will need to use some Java library methods....

  • 4. [Tests basic knowledge of recursion] Write a recursive static Java method that accepts an array...

    4. [Tests basic knowledge of recursion] Write a recursive static Java method that accepts an array arr of integers argument returns a list of all permutations of these integers. (A permutation of a sequence of integers is a re-arrangement of the integers. For example, one permutation of 1, 3, 4, 8, 2 is 3, 1, 2, 8, 4.) For this problem, you may assume that the input array contains no duplicate entries. Your method should return an ArrayList of int...

  • This is code for c++. Any help would be great. #include <iostream> using namespace std; /*...

    This is code for c++. Any help would be great. #include <iostream> using namespace std; /* 1. Create a method that prints your name */ /* 2. Create an overloaded method that takes an integer and prints it out multiplied * by itself * Print out the argument inside the method and then call the method from main * and print it out * What was the typed of passing used inhere ? */ /* 3. Create an overloaded method...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT