0
What is wildcard argument in generics
2 Respostas
+ 10
wildcard (?)
WildCard means any type for instance if you dont no what type of object user will enter from console and you dont know the return type of method than you can use wildcard here us little example
import java.util.*;
public class Program
{
public static void main(String[] args) {
ArrayList<String> a =new ArrayList<>();
fun();
}
static ArrayList<?> fun()
{
return new ArrayList<String>();
}
}
+ 2
//and you can specify general group of types
import java.util.*;
public class Program {
public static void main(String[] args) {
ArrayList<Integer> a =new ArrayList<Integer> (List.of(1,2,3,4) );
ArrayList<Double> b =new ArrayList<Double> (List.of(1.0,2.0,3.0,4.0,5.0) );
print(a);
print(b);
}
static void print( ArrayList<? extends Number> arr) { // here
int i=0;
for(var n: arr)
System.out.println("idx "+ i++ +": "+n);
System.out.println();
}
}