简体   繁体   English

如何使用 Java 中的迭代器从通用数组列表中打印元素

[英]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.我想使用通用 arrays。

Container.java集装箱.java

public interface Container {
     Iterator getIterator();
}

Iterator.java迭代器.java

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

TransactionRepository.java 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>();在我的其他 class 中,我通过首先创建 TransactionRepository object 将元素添加到列表中,如下所示: TransactionRepository<String> companyName = new TransactionRepository<String>(); . . Then I add elements to the array with the add method companyName.add("CompanyName");然后我使用 add 方法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());
        }
  1. With the current implementation List<T> userTransactions is never updated.使用当前实现List<T> userTransactions永远不会更新。

In this case userTransactions.size() in hasNext() method will always return 0 so the result of method will be false .在这种情况下, hasNext()方法中的userTransactions.size()将始终返回0 ,因此方法的结果将为false

  1. Moreover, you should use stringIterator.next() instead of companyName.get() .此外,您应该使用stringIterator.next()而不是companyName.get() Since you implement your own iterator you don't want to use get() method at all.由于您实现了自己的迭代器,因此您根本不想使用get()方法。

  2. There is also a need to update index counter variable after calling next() method.调用next()方法后还需要更新index计数器变量。

     @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.userTransactions上的修饰符更改为private final ,因为它应该只用迭代器引用。

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;
        }
    }
}

It seems that you are never adding elements to your userTransactions List on the add method似乎您从未在 add 方法上将元素添加到您的 userTransactions 列表中

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 add()方法不会向列表中添加任何内容,它就像属性 t 的设置器,您应该使用它来将元素添加到列表中

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:还有另一个问题,索引,你的 next() 方法在你没有初始化你的索引变量时获取索引元素,我建议你这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM