publicclassBeer{publicstaticvoidmain(String[]args){Iterable<Verse>song=newIterable<Verse>(){publicIterator<Verse>iterator(){returnnewSong();}};// All this work to utilize this feature:// "For each verse in the song..."for(Verseverse:song){System.out.println(verse);}}}
Galerinha, como eu converto esse codigo da versão 1.5 para a versão 1.4?
importjava.util.*;publicclassBeer{publicstaticvoidmain(String[]args){// Não existe a interface Iterable em JDK 1.4. Aqui estamos// supondo que "song" seja um objeto de uma classe que contenha um método "iterator"// que retorne um iterador que retorne um "Verse" para cada chamada a "next".// All this work to utilize this feature:// "For each verse in the song..."for(Iteratorit=song.iterator();it.hasNext();){Verseverse=(Verse)it.next();System.out.println(verse);}}}
tifyzinha
A classe song é esta:
classSongimplementsIterator<Verse>{privateintverse=1;publicbooleanhasNext(){returnverse<=100;}publicVersenext(){if(!hasNext())thrownewNoSuchElementException("End of song!");returnnewVerse(verse++);}publicvoidremove(){thrownewUnsupportedOperationException("Cannot remove verses!");}}
como eu poderia converter tb?
T
thingol
//-- Song.javaimportjava.util.*;classSongimplementsIterator{privateintverse=1;publicbooleanhasNext(){returnverse<=100;}publicObjectnext(){if(!hasNext())thrownewNoSuchElementException("End of song!");returnnewVerse(verse++);}publicvoidremove(){thrownewUnsupportedOperationException("Cannot remove verses!");}}//-- Beer.javaimportjava.util.*;publicclassBeer{publicstaticvoidmain(String[]args){// All this work to utilize this feature:// "For each verse in the song..."Songsong=newSong();for(Iteratorit=song;it.hasNext();){Verseverse=(Verse)it.next();System.out.println(verse);}}}
tifyzinha
nossa thiago, vc é bom mesmo hein?? …rsrsrs
5 estrelinhas pra vc… :oops: :oops: :oops: :oops: :oops: