简体   繁体   English

如何检查该点是否是Set的成员?

[英]How can I check if the point is a member on the Set?

I have a class Set , I should read from the Set class couple of points that the user ask. 我有一个Set类,我应该从Set类中读取用户要求的几点。

I have some problems with the method member and subset . 我对方法membersubset有一些问题。

I don't have any idea how it will work, so if any one can give me any examples that I can understand how this method works? 我不知道它如何工作,所以如果有人可以给我提供一些示例,我可以理解这种方法的工作原理?

class Set {
    private point[] p;

    public Set(int n) { 
        p = new point[n];
        readSet(n);
    }

    private void readSet(int n) {
        System.out.println("Please enter " + n + " points");

        for (int i = 0; i < n; i++) {
            int x = 0;
            int y = 0;

            p[i] = new point(x, y);
        }
    }

    public void printSet() {
        for (int i = 0; i < p.length; i++) {
            System.out.print(p[i]);
        } 
    }

    public void shiftSet(int dx, int dy) {
        for (int i = 0; i < p.length; i++) {
            p[i].shift(dx, dy); 
        } 
    }

    // returns if P is in the set, making use of the equals method in class Point
    public boolean member(point P) {
        for (int i = 0; i < p.length; i++) {
            // ...
        }
        return true;
    }

    // returns if current set is a subset of S, making use of the member method
    public boolean subset (Set S) {
        // ...
        return true; 
    }
}

This should be about it, I'll explain it to you: 这应该是关于它,我将向您解释:

public boolean member(point P) {
    // search for P in p
    for (int i = 0; i < p.length; i++) {
        // if it is contained
        if (p[i].equals(P)) {
            // here it is
            return true;
        }
    }

    // couldn't find P
    return false;
}

public boolean subset(Set S) {
    // check for every point in p
    for (int i = 0; i < p.length; i++) {
        // whether it is contained in S
        if (!S.member(p[i])) {
            // can't be a true subset
            return false;
        }
    }

    // everything is as expected
    return true;
}

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

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