Zsolti
Zsolti

Reputation: 75

How to print elements from generic array list, using iterator in Java

I'm working in a school project, where I want to implement the Iterator design pattern. I want to use generic arrays.

Container.java

public interface Container {
     Iterator getIterator();
}

Iterator.java

public interface Iterator <T> {
     boolean hasNext();
     T next();
}

TransactionRepository.java

public class TransactionRepository<T> implements Container {
    public TransactionRepository(){
        userTransactions = new ArrayList<>();
    }
    public List<T> userTransactions;

    @Override
    public Iterator <T> getIterator() {
        return new UserTransactions();
    }

    private T t;

    public void add(T t) {
        this.t = t;
    }

    public T get() {
        return t;
    }

    private class UserTransactions implements Iterator <T> {

        int index;

        @Override
        public boolean hasNext() {
            return index < userTransactions.size();
        }

        @Override
        public T next() {
            if(this.hasNext())
                return userTransactions.get(index);
            return null;
        }
    }
}

In my other class, I add the elements to the list by first creating the TransactionRepository object like this: TransactionRepository<String> companyName = new TransactionRepository<String>();. Then I add elements to the array with the add method companyName.add("CompanyName");. After that I want to print the array using Iterator, but It just won't print the elements. I have tried multiple variations, but none of them worked.

Iterator <String> stringIterator = companyName.getIterator();

        while (stringIterator.hasNext()) {
            System.out.println("Name : " + companyName.get());
        }

Upvotes: 0

Views: 597

Answers (3)

mslowiak
mslowiak

Reputation: 1838

  1. With the current implementation List<T> userTransactions is never updated.

In this case userTransactions.size() in hasNext() method will always return 0 so the result of method will be false.

  1. Moreover, you should use stringIterator.next() instead of companyName.get(). Since you implement your own iterator you don't want to use get() method at all.

  2. There is also a need to update index counter variable after calling next() method.

    @Override
    public T next() {
        if (this.hasNext())
            return userTransactions.get(index++);
        return null;
    }
    
  3. Change modifier on userTransactions to private final as it should be referenced just with iterator.

Code with proposed improvements:

public class TransactionRepository<T> implements Container {
    public TransactionRepository() {
        userTransactions = new ArrayList<>();
    }

    public List<T> userTransactions;

    @Override
    public Iterator<T> getIterator() {
        return new UserTransactions();
    }

    public void add(T t) {
        userTransactions.add(t);
    }

    private class UserTransactions implements Iterator<T> {

        int index;

        @Override
        public boolean hasNext() {
            return index < userTransactions.size();
        }

        @Override
        public T next() {
            if (this.hasNext()) {
                return userTransactions.get(index++);
            }
            return null;
        }
    }
}

Upvotes: 2

Karam Mohamed
Karam Mohamed

Reputation: 881

You add() method doesnt add anything to your list , it's just like a setter of the attribute t , you should use it to add elements to the list instead

public void add(T t) {
    userTransactions.add(t);
}

There is also another problem , the index , your next() method gets the index element while you didnt initialise your index variable , i recommand you to do it in this way :

int index = 0 ;
...
public T next() {
   if(this.hasNext())
          int temp = index;
          index++;
          return userTransactions.get(temp);
        return null;
    }

Upvotes: 1

petrubear
petrubear

Reputation: 721

It seems that you are never adding elements to your userTransactions List on the add method

Upvotes: 1

Related Questions