remet une question vague, je dois deviner beaucoup, mais peut-être c'est quelque chose comme ça vous cherchez:
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;
}
}
Sortie: 1 -> 2 -> 4 -> 5
Ou quelque chose comme ceci:
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;
}
}
qui imprime:
hello world !
Pouvez-vous montrer la définition de la classe? – Phil
Si vous mapper '' IdGroupGroup' à idGroup' et que vous devez rechercher 'idGroup', vous utilisez probablement la structure de données erronées –
Groupe classe {int IDGROUP int idGroupGroup } – Mercer