2012-04-15 49 views
1

我有以下类别:返回一个类的实例

MinMaxArray类:

public class MinMaxArray 
{ 
    public static <T> void MinMax(T[] anArray) 
    { 
    //return an instance of class Pair 
    }//MinMax 
}//class MinMaxArray 

Pair类:

//Two objects 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 

我不知道我怎么可以去返回Pair类的一个实例。不寻找答案,只是一个起点。谢谢

+3

看看'new'运算符。 – Anthales 2012-04-15 11:23:09

+0

@Ahahales它需要折衷数组的最小值和最大值。 “新”运营商会允许我这样做吗? – AkshaiShah 2012-04-15 11:28:02

+0

那么你肯定会需要某种循环来找到最小值和最大值,你可以在一些变量中保存这些值,然后用'new'运算符返回一个'Pair'类的实例,“坚持”最小值和最大值。 – Anthales 2012-04-15 11:32:26

回答

2

一个提示是你可以创建一个对,与T作为两种类型。

相关问题