简体   繁体   English

如何从集合(哈希集)中检索 itrms

[英]How to retrive itrms froma set (hashset)

Good afternoon.下午好。

I am stuck in helping my son in a Java program.我一直在帮助我儿子参加 Java 计划。 Hope someone can help.希望有人能帮忙。 Here is the problem.这就是问题所在。

In Main i have, amoung other.... the read from scanner like this:在主要我有,amoung 其他....从这样的扫描仪读取:

Scanner in = new Scanner(System.in);
    Chord c1 = createChord(in); // este chama o private static Chord createChord(Scanner in)
    Chord c2 = createChord(in);
    Chord c3 = createChord(in);
    Chord c4 = createChord(in);
    
    executeCommand(in);
    executeCommand(in);
    executeCommand(in);
    executeCommand(in);
    
    in.close();
            
}

    
    private static Chord createChord(Scanner in) {
    int n1 = in.nextInt();
    int n2 = in.nextInt();
    int n3 = in.nextInt(); 
    in.nextLine();
    //System.out.print(n1);
    //System.out.print(n2);
    //System.out.print(n3);
    return new Chord (n1, n2, n3);
    
    }

Lets assume the method returns values 1,2 and 3 into the hash Chord.假设该方法将值 1,2 和 3 返回到 hash Chord 中。

The question is How can i, in the program, retrieve the vaules from c1 to c4, for instance, the first or the third element from one of them?问题是我如何在程序中检索 c1 到 c4 的值,例如,其中一个的第一个或第三个元素?

I would greatly appreciate anny help.我将不胜感激任何帮助。

Peter Rodrigues彼得·罗德里格斯

If I understand you correctly your question is how to get the values you passed into Chord ?如果我对你的理解正确,你的问题是如何获得你传递给Chord的值?

You passed those values via the constructor and than you can save them as members in the object of the class. Those members can be made accessible via "Getters".您通过构造函数传递了这些值,然后可以将它们保存为 class 的 object 中的成员。可以通过“Getters”访问这些成员。

Example:例子:

public class Chord {

    final int a;
    final int b;
    final int c;

    public Chord(final int a, final int b, final int c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public int getA() { return a; }

    public int getB() { return b; }

    public int getC() { return c; }

}

You can read up on that eg here .您可以在此处阅读相关内容。

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

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