실제로 이것은 간단 해 보입니다. 그러나 저는 그것에 대해 정말로 혼란스러워하고 있습니다. Java의 열거 hasMoreElements() 및 nextElement() 메서드는 관련되어 있지만 C#의 IEnumerator MoveNext() 및 Current() 속성과는 다르게 작동합니다. 당신이 enumration 클래스의 메소드가 오버로드 내가 MoveNext를하고 현재 모든 곳에서 일하는 것이 함께 hasMoreElements 및 nextElement를 교체 생각하지 않는다 볼 수 있듯이이Java의 열거 형을 C# IEnumerator로 번역
//class declaration, fields constructors, unrelated code etc.
private Vector atomlist = new Vector();
public int getNumberBasis() {
Enumeration basis = this.getBasisEnumeration();
int numberBasis = 0;
while (basis.hasMoreElements()) {
Object temp = basis.nextElement();
numberBasis++;
}
return numberBasis;
}
public Enumeration getBasisEnumeration() {
return new BasisEnumeration(this);
}
private class BasisEnumeration implements Enumeration {
Enumeration atoms;
Enumeration basis;
public BasisEnumeration(Molecule molecule) {
atoms = molecule.getAtomEnumeration();
basis = ((Atom) atoms.nextElement()).getBasisEnumeration();
}
public boolean hasMoreElements() {
return (atoms.hasMoreElements() || basis.hasMoreElements());
}
public Object nextElement() {
if (basis.hasMoreElements())
return basis.nextElement();
else {
basis = ((Atom) atoms.nextElement()).getBasisEnumeration();
return basis.nextElement();
}
}
}
을? : 좋아하지만 어떻게 ... 뭔가를 번역하는 것 때문에 basis.nextElement()는 if-else 문에서 hasMoreElements()를 다시 호출합니다. hasMoreElements를 MoveNext()로 바꾸려면 코드가 하나가 아닌 두 번 진행됩니다.
Enumerable.Concat
감사합니다, 당신은 좀 더 구체적으로 수 없습니다 : 당신은 아마 중 하나 iterator method이 사용하는 거라고? 나는. 코드의 맥락에서 이것이 어떻게 작동하는지 말해 주시겠습니까? – harbm@harbm - 반복자가 필요한 곳이면 어디에서나 비슷한 함수를 사용할 수 있습니다 ... 반복자가 필요한 코드를 표시하면 더 많은 문맥에 넣을 수 있습니다. –