简体   繁体   English

Java:在其他类中使用迭代器

[英]Java: using iterators in other classes

I'm running into problems trying to put a public iterator in a class to read a Map in the class and implementing this iterator in other classes. 我在尝试将公共迭代器放入一个类中以在该类中读取Map并在其他类中实现此迭代器时遇到了问题。 Or in other terms, 或者换句话说

I have class A. Class A contains a private HashMap that I want to access through a public iterator that iterates this map. 我有A类。A类包含一个私有HashMap,我想通过对其进行迭代的公共迭代器进行访问。

I also have class B. I'm trying to run this iterator in class B to read the contents of class A. 我也有B类。我试图在B类中运行此迭代器以读取A类的内容。

This may seem a bit roundabout (or rather, a lot roundabout), but my assignment specified that the data system in class A be hidden to other classes and suggested using a public iterator to access the data. 这似乎有点回旋(或者说很多回旋),但是我的任务指定将A类中的数据系统隐藏到其他类中,并建议使用公共迭代器访问数据。 There is an example of what the method in class B might look like, which I've followed to the best of my ability. 有一个示例说明了B类方法的外观,我已尽力而为。

Can't get it to work, though. 但是,它无法正常工作。

Here's a mockup of the code I have. 这是我拥有的代码的模型。 I tried compiling it, and it works exactly as my real code. 我尝试编译它,它的工作原理与我的实际代码完全相同。

// this is class A

public class Giant {

private Map<Item, Integer> myMap = new HashMap<Item, Integer>();

// add a bunch of items to the map, check if they worked fine

public Iterator<Map.Entry<Item, Integer>> giantIterator = myMap.entrySet().iterator();

}

// and this is in class B

public void receive(Giant mouse){
    System.out.println("I've started!");
        Iterator<Map.Entry<Item, Integer>> foo = mouse.giantIterator;

        while (foo.hasNext()) {
            Map.Entry<Item, Integer> entry = foo.next();
            System.out.println("I'm working!");
        }
}

I also have a testclass which creates objects of either class and then runs the receive method. 我也有一个testclass,它创建任一类的对象,然后运行receive方法。

I receive the message "I've started!" 我收到消息“我已经开始!” but not "I'm working!" 但不是“我在工作!”

At the same time, if I have the two iterators in either class print a toString , the toStrings are identical. 同时,如果我在任一类中都有两个迭代器,则打印toString ,则toStrings是相同的。

I can't simply move the actions I'm supposed to do in class B to class A either, because the iterator is in several different methods and is used for slightly different things in each. 我也不能简单地将本来应该在B类中执行的动作移动到A类,因为迭代器有几种不同的方法,并且在每种方法中用于不同的事情。

I'm a bit stumped. 我有点难过。 Am I missing something from the syntax? 我在语法中缺少什么吗? Am I importing something wrong? 我输入的内容有误吗? Have I just completely messed up how this is supposed to work? 我是否完全搞砸了它应该如何工作? Is this just flat out impossible? 这是完全不可能的吗?

Try exposing the iterator through a function like this: 尝试通过如下函数公开迭代器:

public class Giant {
    private Map<Item, Integer> myMap = new HashMap<Item, Integer>();
    public Iterator<Map.Entry<Item, Integer>> getGiantIterator() {
         return myMap.entrySet().iterator();
    }
}

And in class B change: 并在B类中进行更改:

    Iterator<Map.Entry<Item, Integer>> foo = mouse.giantIterator;

to

Iterator<Map.Entry<Item, Integer>> foo = mouse.getGiantIterator();

That way the iterator will not be created until it is needed. 这样,直到需要时才创建迭代器。

The way you have coded it, the iterator is created while the map is still empty. 按照您的编码方式,在地图仍然为空时创建了迭代器。 I suspect this may be the root of your problem. 我怀疑这可能是您问题的根源。

Your iterator is constructed when A is constructed, and at that time the Map is empty. 您的迭代器是在构造A时构造的,那时Map为空。

Make a method getIterator() that returns an up to date version. 创建一个方法getIterator(),以返回最新版本。

如果您有一个迭代器,并且想要各种类来访问它,则将该变量设置为“ static”。

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

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