我希望在Java中获得泛型类成员(数组)的真正价值。我用Google搜索了几个小时,但没有找到明确的答案。老实说,我是Java和这些通用的新手。
var css = {
'-webkit-animation-iteration-count': "1",
'-moz-animation-iteration-count': "1",
'animation-iteration-count': "1"
}
我已经更新了我的问题。也许这次它更清楚了。
如何获得public class Box<T> {
private T t;
public Box(T t) { this.t = t; }
public void getTheT() {
// get the value of t at index 0
System.out.println(this.t[0]); // this doesn't work
}
}
public class App {
public static void main(String[] args) {
Integer[] Arr = {2,3,4};
Box<Integer[]> i = new Box<Integer[]>(Arr);
i.getTheT();
}
}
的原始值?
答案 0 :(得分:1)
您只需将t
转换为原始类型Integer[]
,如下所示:
public class Box<T>{
private T t;
public Box(T arr){this.t = arr; } // you forget this semi-colon
public void getTheT(){
// get the value of t at index 0
if(t.getClass() == Integer[].class){
System.out.println("t[0]:"+((Integer[])t)[0]); // prints 2
}
else
System.out.println(t);
}
}
public class App{
public static void main(String[] args){
Integer[] Arr = {2,3,4};
Integer i1 = 3;
Box<Integer> i = new Box<Integer>(i1);
i.getTheT();
Box<Integer[]> ia = new Box<Integer[]>(Arr);
ia.getTheT();
}
}
答案 1 :(得分:0)
这显然取决于对象类型。 System.out.println调用的toString的默认实现将返回此类信息。这是真正的价值。如果你想要另一种格式化,你必须手动完成,也可以将数组封装在一个对象中。
class Custom {
private Integer[] arr;
//Constructor
@Override
public String toString(){
String line = "";
for (int i : arr) {
line += i + ",";
}
//take off last ","
return line;
}
}
答案 2 :(得分:0)
&#34;值&#34;这取决于你如何定义它。如果你想打印&#34;值&#34;在您的情况下,Object
您可能希望覆盖toString
方法。由于您无法覆盖数组的实现,因此您必须检查它。有很多库可以为你做这件事(例如在Apache Commons中)。在您的情况下,您可以使用Arrays.deepToString()
方法。
答案 3 :(得分:0)
假设您的Box
将始终保持数组,那么最简单的解决方案是使您的类型参数T
成为元素类型而不是数组类型(例如,Integer
而不是{{ 1}})然后使私有变量的类型为Integer[]
,而不是T[]
。然后T
将始终可以轻松访问数组元素,还可以方便地使用Box
上的任何java.util.Arrays
方法:
toString
问题在于,由于Java泛型目前不能使用基本类型,因此您将不得不忍受对象/盒装类型的开销(例如,public class Box<T> {
private T[] t;
public Box(T[] t) { this.t = t; }
public void getTheT() {
System.out.println(t[0]); // access individual elements
System.out.println(java.util.Arrays.toString(t)); // print the lot
}
}
public class App {
public static void main(String[] args) {
Integer[] Arr = { 2, 3, 4 };
Box<Integer> i = new Box<>(Arr); // not Box<Integer[]> any more
i.getTheT();
}
}
而不是Integer
)。< / p>
如果你想将int
作为数组类型本身(也许是因为你想允许T
,因为Box<int[]>
是不可能的),那么它会变得更加棘手。在这种情况下,您无法使用Box<int>
... [
。
您可以使用java.util.reflect.Array
的方法来访问任意数组&#39;元素无论它们存储在什么变量类型中。这提供了基本的get和set访问权限,但是这些方法有点慢(对于调试来说很好但不是很多):
]
不幸的是,Java目前还没有很好的高效方法来编写可以处理所有数组元素的代码,无论它们的类型如何。相反,当您看到public class Box<T> {
private T t;
public Box(T t) { this.t = t; }
public void getTheT() {
// print all array elements
for (int i = 0, len = java.lang.reflect.Array.getLength(t); i < len; i++) {
System.out.println(java.lang.reflect.Array.get(t, i));
}
}
}
public class App {
public static void main(String[] args) {
int[] Arr = { 2, 3, 4 };
Box<int[]> i = new Box<>(Arr); // primitive array element type!
i.getTheT();
}
}
的方法时,此类代码必须写入九次次。对于元素类型java.util.Arrays
(以及扩展Object
的所有内容),然后为原始元素类型(Object
,boolean
,{{ 1}},byte
,short
,char
,int
,long
)。