Java 返回泛型对
我试图创建一个包含泛型方法的程序,它包含一个类型参数。它应该返回类对的实例。我不知道怎样才能退一双。我的代码如下:Java 返回泛型对,java,generics,instance,Java,Generics,Instance,我试图创建一个包含泛型方法的程序,它包含一个类型参数。它应该返回类对的实例。我不知道怎样才能退一双。我的代码如下: public class MinMaxArray { public static <ArrayType extends Comparable<ArrayType>> ArrayType getMinMax(ArrayType[] anArray) throws IllegalArgumentExce
public class MinMaxArray
{
public static <ArrayType extends Comparable<ArrayType>>
ArrayType getMinMax(ArrayType[] anArray)
throws IllegalArgumentException
{
try
{
ArrayType resultMin = anArray[0];
ArrayType resultMax = anArray[0];
for (int index = 1; index < anArray.length; index++)
if (result.compareTo(anArray[index]) < 0)
result = anArray[index];
if (result.compareTo(anArray[index]) > 0)
result = anArray[index];
return resultMin;
return resultMax;
}//try
catch (ArrayIndexOutOfBoundsException e)
{ throw new IllegalArgumentException("Array must be non-empty", e); }
catch (NullPointerException e)
{ throw new IllegalArgumentException("Array must exist", e); }
}//getMinMax
}//class MinMaxArray
公共类MinMaxArray
{
公共静电
ArrayType getMinMax(ArrayType[]anArray)
抛出IllegalArgumentException
{
尝试
{
ArrayType resultMin=anArray[0];
ArrayType resultMax=anArray[0];
for(int index=1;index
对类代码:
//Two onjects grouped into a pair.
public class Pair<FirstType, SecondType>
{
//The first object.
private final FirstType first;
//The second object.
private final SecondType second;
//Constructor is given the two objects.
public Pair(FirstType requiredFirst, SecondType requiredSecond)
{
first = requiredFirst;
second = requiredSecond;
}//Pair
//Return the first object.
public FirstType getFirst()
{
return first;
}//GetFirst
//Return the second object.
public SecondType getSecond()
{
return second;
}//GetSecond
}//class Pair
//两个对象组合成一对。
公共类对
{
//第一个对象。
私有最终类型优先;
//第二个对象。
私人最终第二类;
//构造函数被赋予两个对象。
公共对(第一种类型需要第一种,第二种类型需要第二种)
{
第一个=需要的第一个;
秒=所需秒;
}//配对
//返回第一个对象。
public FirstType getFirst()
{
先返回;
}//第一
//返回第二个对象。
公共SecondType getSecond()
{
返回第二;
}//第二
}//类对
我不确定如何让resultMax和resultMin成对返回。谢谢你的帮助。也许
public static <ArrayType extends Comparable<ArrayType>>
Pair<ArrayType, ArrayType> getMinMax(ArrayType[] anArray) {
...
return new Pair<ArrayType, ArrayType>(resultMin, resultMax);
}
公共静态
对getMinMax(ArrayType[]anArray){
...
返回新对(resultMin、resultMax);
}
也许
public static <ArrayType extends Comparable<ArrayType>>
Pair<ArrayType, ArrayType> getMinMax(ArrayType[] anArray) {
...
return new Pair<ArrayType, ArrayType>(resultMin, resultMax);
}
公共静态
对getMinMax(ArrayType[]anArray){
...
返回新对(resultMin、resultMax);
}
试试看
或者您可以向Pair类添加工厂方法
public static <FirstType, SecondType> Pair<FirstType, SecondType> of(FirstType first, SecondType second) {
return new Pair<FirstType, SecondType>(first, second);
}
试一试
或者您可以向Pair类添加工厂方法
public static <FirstType, SecondType> Pair<FirstType, SecondType> of(FirstType first, SecondType second) {
return new Pair<FirstType, SecondType>(first, second);
}
我需要使用Pair的一个实例,没有选择余地,所以我将尝试第一个。谢谢,你可以添加一个工厂方法,使它更好我从来没有听说过工厂方法,听起来很有趣,我会仔细研究一下:)当编译器可以推断出类型时,它省去了编写类型的麻烦。工厂方法+1。我确实认为pair类闻起来像是对象拒绝…我需要使用pair的实例,没有选择余地,所以我将尝试第一个。谢谢,你可以添加一个工厂方法,使它更好我从来没有听说过工厂方法,听起来很有趣,我会仔细研究一下:)当编译器可以推断出类型时,它省去了编写类型的麻烦。工厂方法+1。我确实认为pair类闻起来像是对象拒绝…Pavlol,为了测试它,在测试程序的主要方法中,我可以有这样的东西:
String[]aStringArray={“hello”,“what”,“is”,“your”,“name”}
当然,为什么不继续尝试呢?@Pavlov我正在尝试返回最小值和最大值,但我得到的结果是:Pair@10385c1
我在主要测试方法中的代码是:String[]aStringArray={“the”、“cat”、“vaporised”、“on”、“the”、“mat”};对minMaxInAStringArray=MinMaxArray.getMinMax(aStringArray);System.out.println(minmaxinastringaray)
不确定问题出在哪里??您应该在Pair
上定义公共字符串toString()
,以便很好地打印它。像这样:return“[first=“+first+”,second=“+second+”]”
Pavlol,为了测试它,在测试程序的主要方法中,我可以有这样的东西:String[]aStringArray={“hello”,“what”,“is”,“your”,“name”}
当然可以,为什么不试试呢?@Pavlov我正在尝试返回最小值和最大值,但我得到的结果是:Pair@10385c1
我在主要测试方法中的代码是:String[]aStringArray={“the”、“cat”、“vaporised”、“on”、“the”、“mat”};对minMaxInAStringArray=MinMaxArray.getMinMax(aStringArray);System.out.println(minmaxinastringaray)
不确定问题出在哪里??您应该在Pair
上定义公共字符串toString()
,以便很好地打印它。像这样:return“[first=“+first+”,second=“+second+”]”代码>
return Pair.of(resultMin, resultMax);