0

I have a Java file with this variable, and there should be String content in it:

private Set<Set<MyAxiom>> explanations;

I read that Sets are collections of some type of data (which should be MyAxiom, I guess), but couldn't find how to access them, and I need to stamp.

I tried to retrieve these explanations with a get-return method

public Set<Set<OWLAxiom>> getExpl(int index) {
    return this.explanations(index);
}

Similar methods worked out for other normal variables, but I think a Set needs is own commands, doesn't it? And by the way this is a set of sets. I find it really complex. I would be really glad to know how to handle them, and if not, a documentation link would be appreciated.

EDIT: Maybe adding this is useful for your answers. That explanations variable is in a javabean used by a jsp file. It's within a webapp project I compile with maven. The compilation is fine with this code below, but when I access via browser (with Tomcat) I get the error The method getExpl() in the type BundleQueryManagement is not applicable for the arguments (int)

This is the alternate method I tried:

public Set<Set<OWLAxiom>> getExpl() {
    return this.explanations;
}

Or even

public void getExpl() {
    return this.explanations;
}
GondraKkal
  • 87
  • 14
  • Do you want a `Set` returned, or `Axiom` ? – Do Re Feb 23 '17 at 11:48
  • As far as I can see, your method is only valid, if you `return this.explanation;` – Do Re Feb 23 '17 at 11:49
  • [The documentation for `Set`](https://docs.oracle.com/javase/8/docs/api/java/util/Set.html) – khelwood Feb 23 '17 at 11:51
  • Do you mean this.explanationS? I tried this too with ---- public Set> getExpl() { return this.explanations; } But an error occurs. This java class is a javabean used in a jsp page. I use Maven for the compilation and it works fine, but when I try to launch the jsp page i get an error saying "The method getExpl() in the type BundleQueryManagement is not applicable for the arguments (int)" – GondraKkal Feb 23 '17 at 12:01

1 Answers1

2

Your code tells that you have an index value and you want to access corresponding value in your Set. But actually you cannot access Set using index as Sets are unordered collections of objects. So it's not possible.

If you want to access your elements this way, then you should consider using a list instead.

See also: Why doesn't java.util.Set have get(int index)?


EDIT

You can print all elements recursively like this:

public void printExpl(Set<Set<MyAxiom>> explanations) {
    for (Set <MyAxiom> exp: explanations) {
        for (MyAxiom obj: exp) {
            System.out.println(obj.toString());
        }
    }
}
Community
  • 1
  • 1
Raman Sahasi
  • 24,890
  • 6
  • 51
  • 66
  • I want to print out the whole group of elements, which should be String elements – GondraKkal Feb 23 '17 at 11:57
  • @GondraKkal see the edited part of answer. This will print all the elements. Make sure to override `toString()` method appropriately. – Raman Sahasi Feb 23 '17 at 12:06
  • Wait I need to return said elements, so I can print them in a jsp file that will use this method. How does it change? Also, what are exp and obj? I would like to understand more what you did here – GondraKkal Feb 23 '17 at 12:08
  • @GondraKkal `exp` and `obj` are just variable names. I'm printing individual elements. If you want, then you can return them as per your requirements. – Raman Sahasi Feb 23 '17 at 12:10
  • Ok so I will have to modify it in this way: public void printExpl(Set> explanations) { for (Set exp: explanations) { for (MyAxiom obj: exp) { return obj.toString(); } } } – GondraKkal Feb 23 '17 at 12:26
  • Ok so I will have to modify it in this way: public void printExpl(Set> explanations) { for (Set exp: explanations) { for (MyAxiom obj: exp) { return obj.toString(); } } } EDIT: @Raman Sahasi now that I think about it, I cannot have multiple returns, so how can I group all my variables in an unique one? – GondraKkal Feb 23 '17 at 14:14
  • Tried your code, but this occurred: [ERROR] /.../myProgram.java:[lineOfTheError] incompatible types: unexpected return value – GondraKkal Feb 23 '17 at 14:39
  • @GondraKkal I'm just printing value in my method, that's why I've given return type `void`. If you want a value in return type, then it's obvious that you should change the return type appropriately. – Raman Sahasi Feb 24 '17 at 04:14
  • Even putting 'void' return me an error, that what I meant. How can I modify the code with no stamp and returning the value in explanations instead? – GondraKkal Feb 24 '17 at 08:35