引用实例引用实例

Referring to an instance that refers to an instance

我不确定这个问题的标题是否正确。

我有这个学校作业,我们必须创建两个 classes。

在一个 class 中,我们定义人与人之间的关系,例如A 认识 B,而在另一个 class 中,我们会对此提出问题,例如A认识B吗?

下面第一个 class 定义关系并给出方法,第二个 class 查询它们。

我确定我的错误出在 public 布尔值 'knowsWithDegree' 的某处。你能帮忙吗?

public class SocialGraph {

    private HashMap<String, List<String>> map = new HashMap<String, List<String>>();
    
    public SocialGraph() {   // empty constructor
        map = new HashMap<String, List<String>>();
    }
    
    public void addIndividual(String a) {
        if (!map.containsKey(a)) {
            map.put(a, new ArrayList<String>());
        } else {
    
        }
    }
    
    public boolean hasKnowsArrow(String a, String b) {
        if (map.containsKey(a)) {
            return map.get(a).contains(b);
        } else {
            return false;
        }
    }
    
    public void addKnowsArrow(String a, String b) {
    
        if ((!map.containsKey(a) || !map.containsKey(b)) || (hasKnowsArrow(a, b))) {
        } else {
            map.get(a).add(b);
        }
    }
    
    public void removeKnowsArrow(String a, String b) {
    
        if ((!map.containsKey(a) || !map.containsKey(b)) || (!hasKnowsArrow(a, b))) {
        } else {
            map.get(a).remove(b);
        }
    }
    
    public boolean knowsWithDegree(String a, String b, int x) {
        Object[] keys = map.keySet().toArray();
    
        int y;
        y = 0;
    
        if (map.get(a).contains(b)) {
            y = 1;
        } else {
    
            if ((map.get(a).contains(map.get(keys[0]).contains(b))) || (map.get(a).contains(map.get(keys[1]).contains(b))) ||
                    (map.get(a).contains(map.get(keys[2]).contains(b))) || (map.get(a).contains(map.get(keys[3]).contains(b)))) {
                y = 2;
            }
        }
        if (x == y) {
            return true;
        } else
            return false;
        }
    }
    
    
    public class SocialGraphTest {
    
        public static void main(String[] args) {
            SocialGraph socialGraph = new SocialGraph();
            socialGraph.addIndividual("Anne");
            socialGraph.addIndividual("Daisy");
            socialGraph.addIndividual("Bob");
            socialGraph.addIndividual("Charlie");
        
        
            socialGraph.addKnowsArrow("Anne", "Bob");
            socialGraph.addKnowsArrow("Anne", "Daisy");
            socialGraph.addKnowsArrow("Bob", "Daisy");
            socialGraph.addKnowsArrow("Bob", "Charlie");
        
            System.out.println(socialGraph.hasKnowsArrow("Anne", "Bob")); //should be true
            System.out.println(socialGraph.hasKnowsArrow("Anne", "Daisy"));//should be true
            System.out.println(socialGraph.hasKnowsArrow("Bob", "Daisy"));//should be true
            System.out.println(socialGraph.hasKnowsArrow("Bob", "Charlie"));//should be true
            System.out.println(socialGraph.hasKnowsArrow("Anne", "Charlie")); //should be false
        
            System.out.println ();
        
            System.out.println (socialGraph.knowsWithDegree ("Anne", "Daisy", 1));
            System.out.println (socialGraph.knowsWithDegree ("Anne", "Charlie", 2));
            System.out.println (socialGraph.knowsWithDegree ("Anne", "Daisy", 3));
        
        }
    }
}

下面是一个使用递归的例子:



        public boolean knowsWithDegree(String a, String b, int x) {
            return knowsWithDegreeRecursive(a, b, x, new HashSet<>());
        }
        
        private boolean knowsWithDegreeRecursive(String a, String b, int x, Set<String> visited) {
            if (x < 1) {
                // x must be at least 1
                return false;
            }
            

            if (map.get(a).contains(b)) {
                // If a knows b, then the number of degrees should be 1
                return x == 1;
            }
            
            if (x == 1) {
                // Since the degree is 1 and a does not know b, then a does not know b to the specified degree
                return false;
            }
            
            // Go through each person that a knows
            for (String c : map.get(a)) {
                
                if (visited.contains(c)) {
                    // We've already checked this person
                    continue;
                }
                
                // Mark c as visited so we don't check them again
                visited.add(c);
                
                // See if this person knows b, with one fewer degree
                // e.g. if we're seeing if a knows b with a degree of 2, then c should know b with a degree of 1
                boolean knowsWithDegree = knowsWithDegreeRecursive(c, b, x - 1, visited);
                
                // If c knows b with the degree minus 1, then a knows b with the specified degree
                if (knowsWithDegree) {
                    return true;
                }
            }
            
            // a does not know b to the specified degree
            return false;
        }
        

如果 knowsArrows 的顺序无关紧要,我建议在 map 中使用 HashSet 而不是 ArrayList