Java 问题遍历类的树形图集合

发布于 2024-12-03 10:04:38 字数 346 浏览 2 评论 0原文

我尝试使用以下方法迭代 Tile() 类的 Treemap:

Map map = new TreeMap();

Iterator itr = world.map.values().iterator();

while(itr.hasNext()){
     Tile t = ???;
     System.out.print(t.xCord+","+t.yCord+","+t.zCord);
}

如何在迭代器中获取对象 Tile 的实例?或者如果有更好的方法可以做到这一点,请告诉我。

我试过用谷歌搜索这个,但我总是最终寻找迭代纯数据的方法,比如字符串等,而不是类的实例化......

Im trying to iterate through a Treemap of the class Tile() using:

Map map = new TreeMap();

Iterator itr = world.map.values().iterator();

while(itr.hasNext()){
     Tile t = ???;
     System.out.print(t.xCord+","+t.yCord+","+t.zCord);
}

How do I get the instance of the object Tile within the iterator? Or if theres some better way of doing this, plz let me know.

I've tried googling this, but I always end up looking at ways to iterate through pure data, like strings etc, and not Instanciations of class'...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

紫罗兰の梦幻 2024-12-10 10:04:38

字符串不是 Java 中的纯数据,它们是对象,因此是相同的:

Map map = new TreeMap();

Iterator itr = world.map.values().iterator();

while(itr.hasNext()){
    Tile t = (Tile)itr.next();
    System.out.print(t.xCord+","+t.yCord+","+t.zCord);
}

或更好:

Map<Key, Tile> map = new TreeMap<Key, Tile>();

Iterator<Tile> itr = world.map.values().iterator();

while(itr.hasNext()){
    Tile t = itr.next();
    System.out.print(t.xCord+","+t.yCord+","+t.zCord);
}

甚至更好:

Map<Key, Tile> map = new TreeMap<Key, Tile>();

for(Tile t : map.values){
    System.out.print(t.xCord+","+t.yCord+","+t.zCord);
}

ps Key 是您正在使用的关键对象的类

Strings aren't pure data in Java, they are objects, so is the same:

Map map = new TreeMap();

Iterator itr = world.map.values().iterator();

while(itr.hasNext()){
    Tile t = (Tile)itr.next();
    System.out.print(t.xCord+","+t.yCord+","+t.zCord);
}

Or better:

Map<Key, Tile> map = new TreeMap<Key, Tile>();

Iterator<Tile> itr = world.map.values().iterator();

while(itr.hasNext()){
    Tile t = itr.next();
    System.out.print(t.xCord+","+t.yCord+","+t.zCord);
}

or even better:

Map<Key, Tile> map = new TreeMap<Key, Tile>();

for(Tile t : map.values){
    System.out.print(t.xCord+","+t.yCord+","+t.zCord);
}

p.s Key is the class of the key objects you're using

爱的那么颓废 2024-12-10 10:04:38
Tile t = (Tile) itr.next();

但请注意,使用迭代器(尤其是原始集合)是一种非常过时的 Java 编写方式。更好的是使用类型化集合和增强的 for 循环:

Map<String, Tile> map = new TreeMap<String, Tile>();

for(Tile t : map.values()){
     System.out.print(t.xCord+","+t.yCord+","+t.zCord);
}
Tile t = (Tile) itr.next();

But note that using Iterators and especially raw collections is a very outdated way of writing Java. Much better would be using typed collections and the enhanced for loop:

Map<String, Tile> map = new TreeMap<String, Tile>();

for(Tile t : map.values()){
     System.out.print(t.xCord+","+t.yCord+","+t.zCord);
}
謌踐踏愛綪 2024-12-10 10:04:38

itr.next() 给出下一个元素的实例,如 javadocs。请注意,由于您的地图是原始类型,因此您需要进行强制转换:Tile t = (Tile)itr.next();,但它不是类型安全的。

更好的解决方案是使用泛型,如 @Simone 建议的那样

itr.next() gives the instance of the next element, as specified in the javadocs. Note that since your Map is of raw type, you will need a cast: Tile t = (Tile)itr.next(); but it is NOT type safe.

Even better solution is using generics as @Simone suggested

梦幻的心爱 2024-12-10 10:04:38

但是,在您使用的迭代器中

while(itr.hasNext()){
     Tile t = itr.next(); ...

,除非您使用泛型提供了类型,否则您还必须强制转换图块:

Map<Whatever,Tile> map = new TreeMap<Whatever,Tile>();
Iterator<Tile> itr = world.map.values().iterator();

否则必须强制转换迭代器值 - 即您必须将示例(以及上面的行)更正为

while(itr.hasNext()){
         Tile t = (Tile)itr.next(); ...

In an iterator you use

while(itr.hasNext()){
     Tile t = itr.next(); ...

However, you'd also have to cast the tile unless you gave the type with generics:

Map<Whatever,Tile> map = new TreeMap<Whatever,Tile>();
Iterator<Tile> itr = world.map.values().iterator();

Otherwise the iterator values will have to be cast - i.e. you'd have to correct your example (and the lines above) to

while(itr.hasNext()){
         Tile t = (Tile)itr.next(); ...
别想她 2024-12-10 10:04:38

在循环中,使用 itr.next()Iterator 检索下一个 Tile 对象。目前,这将返回一个 Object,因为您使用的是原始类型的 TreeMap。而是使用 TreeMap,以便值迭代器将是 Iterator

Within the loop, use itr.next() to retrieve the next Tile object from the Iterator. Currently this will return an Object, since you are using a raw-typed TreeMap. Instead use TreeMap<Key, Tile>, so that the values iterator will be an Iterator<Tile>.

优雅的叶子 2024-12-10 10:04:38

Tile t = (Tile) itr.next()

这将为您提供该类的下一个实例,并将迭代器移动到下一个实例。

Tile t = (Tile) itr.next()

This will give you the next instance of the class as well as move the iterator to the next instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文