import java.util.Scanner; import java.io.File; import java.util.Iterator; public class Pokedex implements Iterable{ public static void main(String[] args) { System.out.println("Hei jeg er main!"); Pokedex pokedex = new Pokedex(); pokedex.lesInnFraFil("PokemonList.csv"); /* for (Pokemon pokemon : pokedex) { if (pokemon.getDexnr() <= 151){ pokemon.prettyPrint(); } } */ pokedex.skrivUtBaklengs(); } //PokeNode forste = null; PokeNode forste; PokeNode siste; public void skrivUtBaklengs() { rekursivSkrivUtBaklengs(forste); } private void rekursivSkrivUtBaklengs(PokeNode denne) { if (denne == null) { return; } rekursivSkrivUtBaklengs(denne.neste); denne.pokemon.prettyPrint(); } public void skrivUt() { rekursivSkrivUt(forste); } private void rekursivSkrivUt(PokeNode denne) { // basis-tilfelle // N?R SKAL VI STOPPE? if (denne == null){ return; } // Logikk-del denne.pokemon.prettyPrint(); rekursivSkrivUt( denne.neste ); } public Iterator iterator() { return new MinIterator(); } private class MinIterator implements Iterator { PokeNode current = forste; // next() @Override public Pokemon next() { Pokemon returmonster = current.pokemon; current = current.neste; return returmonster; } @Override public boolean hasNext() { /* if (current != null) { return true; } else { return false; } */ return current != null; } // hasNext() } // Setter inn en pokemon i listen. public void settInnPokemon(Pokemon pokemon) { PokeNode nyPokemonNode = new PokeNode(pokemon); if (forste == null) { forste = nyPokemonNode; } else { siste.neste = nyPokemonNode; } siste = nyPokemonNode; } public void lesInnFraFil(String filnavn) { Scanner filScanner = null; try { filScanner = new Scanner(new File(filnavn)); } catch (Exception e){ System.out.println("Filen finnes ikke!"); System.exit(0); } filScanner.nextLine(); while(filScanner.hasNextLine()) { String linje = filScanner.nextLine(); //System.out.println(linje); String[] data = linje.split(","); data[0] = data[0].replace(" #", ""); Pokemon nyPokemon = null; if (data.length == 3) { nyPokemon = new Pokemon(data[0], data[1], data[2]); } else if (data.length == 4) { nyPokemon = new Pokemon(data[0], data[1], data[2], data[3]); } //nyPokemon.prettyPrint(); settInnPokemon(nyPokemon); } } private class PokeNode { public Pokemon pokemon; public PokeNode neste; public PokeNode(Pokemon pokemon) { this.pokemon = pokemon; } } }