나는 이라는 쌍의 추상 데이터 형식 인 Java에서 쌍을 만들고 있습니다. 두 개의 객체를 가져 와서이 데이터 유형으로 그룹화해야합니다. 이것은 30 분 미만 걸릴 것으로 예상되지만, 나는 3 시간 반 동안 그 일을 해왔다. 나는 처음 두 가지 방법이 옳다고 믿지만, 나는 반전 또는 같음을 알 수 없다. 당신은 내가 코드에서 시도한 볼 수 있습니다 : 모든두 객체를 쌍으로 만드는 ADT를 만들려고합니다.
public class Pair<T1,T2> implements PairInterface<T1,T2>
{
// TO DO: Your instance variables here.
public T1 first;
public T2 second;
public Pair(T1 aFirst, T2 aSecond)
{
first = aFirst;
second = aSecond;
}
/* Gets the first element of this pair.
* @return the first element of this pair.
*/
public T1 fst()
{
return this.first;
}
/* Gets the second element of this pair.
* @return the second element of this pair.
*/
public T2 snd()
{
return this.second;
}
/* Gets a NEW pair the represents the reversed order
* of this pair. For example, the reverse order of the
* pair (x,y) is (y,x).
* @return a NEW pair in reverse order
*/
public PairInterface<T2,T1> reverse()
{
return PairInterface<this.snd(),this.fst()>;//Pair<second;first>
}
/* Checks whether two pairs are equal. Note that the pair
* (a,b) is equal to the pair (x,y) if and only if a is
* equal to x and b is equal to y.
*
* Note that if you forget to implement this method, your
* compiler will not complain since your class inherits this
* method from the class Object.
*
* @param otherObject the object to be compared to this object.
* @return true if this pair is equal to aPair. Otherwise
* return false.
*/
public boolean equals(Object otherObject)
{
if(otherObject == null)
{
return false;
}
if(getClass() != otherObject.getClass())
{
return false;
}
if (otherObject.fst.equals(this.fst) &&
otherObject.snd.equals(this.snd))
{
return true;
} else {
return false;
}
}
/* Generates a string representing this pair. Note that
* the String representing the pair (x,y) is "(x,y)". There
* is no whitespace unless x or y or both contain whitespace
* themselves.
*
* Note that if you forget to implement this method, your
* compiler will not complain since your class inherits this
* method from the class Object.
*
* @return a string representing this pair.
*/
public String toString()
{
return "("+first.toString()+","+second.toString()+")";
}
}
_ "역 자명을 찾을 수 없습니까?"_ 무엇을 의미합니까? 좀 더 자세히 설명해주십시오. 또한 [FAQ] 및 [Ask]를 읽으십시오. 서면으로하는 질문은 "나를 위해 일해주세요."와 너무 비슷합니다. –
글쎄, 나는 equals로 시작할 것이다. Im은 다른 쌍이이 쌍과 같은지를 결정하는 데 사용된다고 가정하므로 개체와 쌍을 가져 가지 않는 이유는 무엇입니까? 임의의 객체를 한 쌍으로 어떻게 비교할 수 있습니까? –
까지 반대로, 그 pairInterface를 반환해야하기 때문에 내가 무슨 일을해야한다고 생각하지만 컴파일러는 this.snd()와 this.fst() 세미콜론 inbetween을 요구하고있다. 왜 그런가요? –