Conversão de código do jdk 1.5 para a 1.4

4 respostas
tifyzinha
public class Beer {
    public static void main(String[] args ) {

        Iterable<Verse> song= new Iterable<Verse>() {
            public Iterator<Verse> iterator() {
                return new Song();
            }
        };

        // All this work to utilize this feature:
        // "For each verse in the song..."

        for(Verse verse : song) {
            System.out.println(verse);
        }
    }
}

Galerinha, como eu converto esse codigo da versão 1.5 para a versão 1.4?

bjos

4 Respostas

T
import java.util.*;
public class Beer {
    public static void main(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 (Iterator it = song.iterator(); it.hasNext(); ) {
            Verse verse = (Verse) it.next();
            System.out.println(verse);
        }
    }
}
tifyzinha

A classe song é esta:

class Song implements Iterator<Verse> {
    private int verse=1;

    public boolean hasNext() {
        return verse <= 100;
    }

    public Verse next() {
        if(!hasNext()) 
            throw new NoSuchElementException("End of song!");
        return new Verse(verse++);
    }

    public void remove() {
        throw new UnsupportedOperationException("Cannot remove verses!");
    }
}

como eu poderia converter tb?

T
//-- Song.java
import java.util.*;
class Song implements Iterator {
     private int verse = 1;
 
     public boolean hasNext() {
         return verse <= 100;
     }
 
     public Object next() {
         if(!hasNext()) 
             throw new NoSuchElementException("End of song!");
         return new Verse(verse++);
     }
 
     public void remove() {
         throw new UnsupportedOperationException("Cannot remove verses!");
     }
 }


//-- Beer.java
import java.util.*;
public class Beer {
    public static void main(String[] args ) {
        // All this work to utilize this feature:
        // "For each verse in the song..."
        Song song = new Song();
        for (Iterator it = song; it.hasNext(); ) {
            Verse verse = (Verse) it.next();
            System.out.println(verse);
        }
    }
}
tifyzinha

nossa thiago, vc é bom mesmo hein?? …rsrsrs

5 estrelinhas pra vc… :oops: :oops: :oops: :oops: :oops:

Criado 3 de maio de 2006
Ultima resposta 3 de mai. de 2006
Respostas 4
Participantes 2