Untitled
Anonymous
plain_text
04/15/2024 12:39 PM
800 B
14
Indexable
package JavaCollection;
/* write a single method printArray that can print all the elements of both arrays.
The method should be able to accept both integer arrays or string arrays.
*/
import java.util.Arrays;
public class JavaGenerics {
public static void main(String[] args) {
Integer array[] = {4, 5, 6};
String words[] = {"hello", "hi", "how"};
printArray(array);
printArray(words);
}
public static <T> void printArray(T[] arr) {
for (T element : arr) {
System.out.println(element);
}
}
}
Editor is loading...
Leave a Comment