帮助在 Java 中使用递归

发布于 2024-09-02 00:08:12 字数 492 浏览 2 评论 0原文

我有一个类Group。在类中,我有两个字段,idGroup IdGroupGroup。组可以是其他组的一部分。我的类 Group 是在 HashMap 中定义的;键是 IdGroupGroup,值是 idGroup。我想在地图中搜索特定的idGroup;我可以使用递归来做到这一点吗?

class Group
{

    int idGroupe
    String word
}

HashMap<Integer,Integer> GroupeGroupe = new HashMap<Integer,Integer>();
GroupeGroupe.put(idGroupeGroupe, idGroupe)

I have a class Group. In the class I have two fields, idGroup IdGroupGroup. Groups may be part of other groups. My class Group is defined in a HashMap<Integer,Integer>; the key is IdGroupGroup and value is idGroup. I want to search the map for a particular idGroup; can I use recursion to do this?

class Group
{

    int idGroupe
    String word
}

HashMap<Integer,Integer> GroupeGroupe = new HashMap<Integer,Integer>();
GroupeGroupe.put(idGroupeGroupe, idGroupe)

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

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

发布评论

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

评论(2

祁梦 2024-09-09 00:08:12

给出一个模糊的问题,我必须猜测很多,但也许您正在寻找这样的东西:

import java.util.*;

public class Test {

    static Map<Integer, Integer> groups = new HashMap<Integer, Integer>();

    public static void main(String... args) {
        groups.put(1, 2);
        groups.put(2, 3);
        groups.put(2, 4);
        groups.put(4, 5);
        System.out.println(searchFor(1, 5));
    }

    private static String searchFor(int from, int target) {

        // Target found?
        if (from == target) return "" + target;

        // Dead end?
        if (!groups.containsKey(from)) return null;

        // Recurse and try to find it from here.
        String path = searchFor(groups.get(from), target);
        return path == null ? null : from + " -> " + path;
    }
}

输出:
1-> 2-> 4-> 5


或者类似这样:

    static Map<Integer, Group> groups = new HashMap<Integer, Group>();

    public static void main(String... args) {
        groups.put(0, new Group(1, "hello")); // (0: -)       -> (1: "hello")
        groups.put(2, new Group(9, "!"));     // (2: "world") -> (9, "!")
        groups.put(3, new Group(5, "bye"));   // (3: -)       -> (5, "bye")
        groups.put(1, new Group(2, "world")); // (1: "hello") -> (2: "world")
        System.out.println(traverse(0));
    }

    private static String traverse(int from) {
        if (!groups.containsKey(from)) return "";

        String path = traverse(groups.get(from).idGroupe);
        return path == null ? null : groups.get(from).word + " " + path;
    }
}

打印:

hello world ! 

Given a vague question, I have to guess a lot, but perhaps it's something like this you're looking for:

import java.util.*;

public class Test {

    static Map<Integer, Integer> groups = new HashMap<Integer, Integer>();

    public static void main(String... args) {
        groups.put(1, 2);
        groups.put(2, 3);
        groups.put(2, 4);
        groups.put(4, 5);
        System.out.println(searchFor(1, 5));
    }

    private static String searchFor(int from, int target) {

        // Target found?
        if (from == target) return "" + target;

        // Dead end?
        if (!groups.containsKey(from)) return null;

        // Recurse and try to find it from here.
        String path = searchFor(groups.get(from), target);
        return path == null ? null : from + " -> " + path;
    }
}

Output:
1 -> 2 -> 4 -> 5


Or something like this:

    static Map<Integer, Group> groups = new HashMap<Integer, Group>();

    public static void main(String... args) {
        groups.put(0, new Group(1, "hello")); // (0: -)       -> (1: "hello")
        groups.put(2, new Group(9, "!"));     // (2: "world") -> (9, "!")
        groups.put(3, new Group(5, "bye"));   // (3: -)       -> (5, "bye")
        groups.put(1, new Group(2, "world")); // (1: "hello") -> (2: "world")
        System.out.println(traverse(0));
    }

    private static String traverse(int from) {
        if (!groups.containsKey(from)) return "";

        String path = traverse(groups.get(from).idGroupe);
        return path == null ? null : groups.get(from).word + " " + path;
    }
}

Which prints:

hello world ! 
心病无药医 2024-09-09 00:08:12

我不确定我是否理解你的问题,但我会尽力回答。
如果您有一个带有条目的 java HashMap,我对您的理解是否正确?
在这种情况下,每个 idGroupGroup (java api) 只有一个条目:

   public V put(K key,V value)

将指定值与
该映射中的指定键。如果
地图以前包含一个映射
键,旧值被替换。

如果这没问题,并且您只想对可以使用的所有元素进行递归:

public Collection<V> values()

返回集合视图
该地图中包含的值。这
集合是由地图支持的,所以
地图的更改反映在
集合,反之亦然。如果
迭代时修改地图
以上征集正在进行中
(除非通过迭代器自己的
删除操作),结果
迭代未定义。这
集合支持元素删除,
这会删除相应的
从地图映射,通过
迭代器.删除,集合.删除,
删除全部、保留全部和清除
运营。它不支持
添加或添加所有操作。

看看:
http://java.sun.com/javase/ 6/docs/api/java/util/HashMap.html

干杯,
约尔根

I'm not sure I understand your question, but I'll try to answer.
Do I understand you correctly if you have a java HashMap with entries ?
In that case you only have one entry for each idGroupGroup (java api):

   public V put(K key,V value)

Associates the specified value with
the specified key in this map. If the
map previously contained a mapping for
the key, the old value is replaced.

if this is fine, and you just want to do a recursion of all element you can use:

public Collection<V> values()

Returns a Collection view of the
values contained in this map. The
collection is backed by the map, so
changes to the map are reflected in
the collection, and vice-versa. If the
map is modified while an iteration
over the collection is in progress
(except through the iterator's own
remove operation), the results of the
iteration are undefined. The
collection supports element removal,
which removes the corresponding
mapping from the map, via the
Iterator.remove, Collection.remove,
removeAll, retainAll and clear
operations. It does not support the
add or addAll operations.

have a look at:
http://java.sun.com/javase/6/docs/api/java/util/HashMap.html

cheers,
Jørgen

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