英文:
How to initialize a generic variable
问题
我在使用泛型方面遇到了问题。我有一个类如下:
public class findMinMax<T extends Comparable<T>> {
private T min;
private T max;
public findMinMax() {
// 需要将 min 和 max 初始化为某个值,以便进行比较
}
public void find(T value) {
if (min == null || min.compareTo(value) > 0)
min = value;
if (max == null || value.compareTo(max) > 0)
max = value;
}
}
那么,你如何初始化一个泛型变量呢?谢谢。
英文:
I'm having trouble working with generics. I have a class as
public class findMinMax<T extends Comparable <T>> {
private T min;
private T max;
public findMinMax() {
//Need to initialize min max to something so that I can compare them
}
public void find(T value) {
if (min == null || min.compareTo(value) > 0)
min = value;
if (max == null ||value.compareTo(max) > 0)
max = value;
}
}
So how can you initialize a generic variable? Thanks
答案1
得分: 4
你可以将它们初始化为 null
(实际上,默认情况下它们将是 null
,所以不需要显式初始化),但是然后你需要在你的 find
方法中添加 null
检查:
public class findMinMax<T extends Comparable<T>> {
private T min;
private T max;
public findMinMax() {
}
public void find(T value) {
if (min == null || min.compareTo(value) > 0)
min = value;
if (max == null || value.compareTo(max) > 0)
max = value;
}
}
英文:
You can initialize them to null
(actually they will be null
by default, so no need to explicitly initialize them), but then you'll need to add null
checks in your find
method:
public class findMinMax<T extends Comparable <T>> {
private T min;
private T max;
public findMinMax() {
}
public void find(T value) {
if (min == null || min.compareTo(value) > 0)
min = value;
if (max == null || value.compareTo(max) > 0)
max = value;
}
}
答案2
得分: 1
通用信息仅在编译时使用。'T' 及其含义在已编译(和运行中的)代码中已消失。
但你可以通过构造函数传递类,并以通用方式创建实例:
public class findMinMax<T extends Comparable<T>> {
private T value;
private T min;
private T max;
public findMinMax(Class<T> aClass)
{
value = aClass.newInstance();
min = aClass.newInstance();
max = aClass.newInstance();
}
// 其他代码...
}
具体的类 T 需要一个无参数构造函数。
英文:
The generic information is only used at compile time. The 'T' and its meaning has disappeared in compiled (and running) code.
But you can pass the class by constructor, and create an instance in a generic way:
public class findMinMax<T extends Comparable <T>> {
private T value;
private T min;
private T max;
public findMinMax(Class<T> aClass)
{
value = aClass.newInstance();
min = aClass.newInstance();
max = aClass.newInstance();
}
....
}
The concrete class T needs a parameterless constructor.
答案3
得分: 0
可比较性支持空值。但是你的代码不支持。
如果你想使你的代码支持空值,使用以下代码:
public class FindMinMax<T extends Comparable<T>> {
private T min;
private T max;
private boolean firstCall = true;
public void find(T value) {
if (firstCall) {
min = value;
max = value;
firstCall = false;
} else if (value != null) {
if (value.compareTo(min) < 0) {
min = value;
}
if (value.compareTo(max) > 0) {
max = value;
}
} else {
if (min != null && min.compareTo(value) > 0) {
min = value;
}
if (max != null && max.compareTo(value) < 0) {
max = value;
}
}
}
}
英文:
Comparable is null
-friendly. But your code is not.
If you want to make your code null
-friendly, use this:
public class FindMinMax<T extends Comparable <T>> {
private T min;
private T max;
private boolean firstCall = true;
public void find(T value) {
if (firstCall) {
min = value;
max = value;
firstCall = false;
} else if (value != null) {
if (value.compareTo(min) < 0) {
min = value;
}
if (value.compareTo(max) > 0) {
max = value;
}
} else {
if (min != null && min.compareTo(value) > 0) {
min = value;
}
if (max != null && max.compareTo(value) < 0) {
max = value;
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论