Looking at the methods of the Array class on libgdx. I found this method:
public void addAll (T... array) {
addAll(array, 0, array.length);
}
I had never seen that "T..." before, and it turns out that it's incredibly hard to search for "T..." either on Google or here on Stack Overflow. I think I understand generics, but the "..." is new to me.
What does that mean? So, for example, if T is String, then how would I use this method? Why would I use it? And how would that be different from using "T[]" instead?
\t Insert a tab in the text at this point. \b Insert a backspace in the text at this point. \n Insert a newline in the text at this point. \r Insert a carriage return in the text at this point.
In java <T> means Generic class. A Generic Class is a class which can work on any type of data type or in other words we can say it is data type independent.
T is a simple object-oriented programming language, modeled on Java. T supports only one primitive type, the integer type. T also supports reference types via the class Object, which is the root of the inheritance hierarchy. T supports only single-inheritance. T syntax is derived from Java syntax.
Delta Fare class T is a revenue fare/booking class of service that is marketed as Main Cabin on Delta Air Lines mainline and code share flights. This fare class is considered a deeply discounted main cabin fare class that is eligible for complimentary upgrades.
The T... is just a varargs parameter, where the element type happens to be T, the generic type parameter of the class.
The point is that you can call the method like this (assuming array is an Array<String>):
array.addAll("x", "y", "z");
which will be equivalent to
array.addAll(new String[] { "x", "y", "z" });
This can be used without generics too. For example:
public static int sum(int... elements) {
int total = 0;
for (int element : elements) {
total += element;
}
return total;
}
Varargs parameters were introduced in Java 5.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With