0

I'm just starting out with Java and have some fairly basic questions about sets. I want to create a class called DirectedGraph which has two attributes, a set of vertices and a set of edges. What would be the best data structure to use for these sets? It seems Java has several implementations of sets and I would probably just want to use the most basic, featureless kind.

Also, when I try to set the attributes vertices and edges I get errors like "Set cannot be resolved by a type" or "Syntax error, insert "Dimensions" to complete Reference Type". Here is some code along with other things I tried commented out:

import java.util.Collections;

public class DirectedGraph {
    private Set<> vertices;

    // here are some other guesses that also didn't work:
    // private Set<int[]> vertices;
    // private Set<Integer> vertices;
    // private Set<Integer> vertices = new HashSet<Integer>();
}

What is going on with these errors?

Edit:

And if I wanted a constructor that took no arguments but initialized the vertices and edges sets as empty, how would I do this?

dsaxton
  • 995
  • 2
  • 10
  • 23
  • 3
    You are just missing the import. `import java.util.Set` – Tunaki Dec 31 '15 at 14:38
  • Thanks, and I need to use Integer rather than int? I had tried importing java.util.Set before, but I guess I was using the wrong code inside the class when I did that. – dsaxton Dec 31 '15 at 14:40
  • @Tunaki I added another question about how to initialize empty sets. If you wanted to move your comment to an answer with or without an answer to this part I'd accept it. – dsaxton Dec 31 '15 at 14:44
  • 1
    http://stackoverflow.com/questions/2504959/why-can-java-collections-not-directly-store-primitives-types – sinclair Dec 31 '15 at 14:44
  • 1
    Collections can only hold reference types. Not primitive types. Integer is a reference type, int[] is a reference type, but int is a primitive type. To initialize an empty, mutable set, you can use `new HashSet<>()` or `new TreeSet<>()` (or any other set implementation you desire to use). For an immutable empty set, use `Collections.emptySet()`. All your questions are covered by the Java tutorial: https://docs.oracle.com/javase/tutorial/collections/ and by the javadoc: https://docs.oracle.com/javase/8/docs/api/java/util/Set.html – JB Nizet Dec 31 '15 at 14:45
  • @JBNizet If I use something like `this.vertices = new HashSet<>();` in the DirectedGraph constructor I get an error that HashSet cannot be resolved to type and it doesn't go away when I use `HashSet()`. Any idea as to what's going on? – dsaxton Dec 31 '15 at 14:49
  • 1
    @dsaxton Read my answer carefully. Every class outside of `java.lang` needs to be imported. `HashSet` is one of them ;). – Tunaki Dec 31 '15 at 14:50
  • And read the tutorial aboput packages as well: https://docs.oracle.com/javase/tutorial/java/package/usepkgs.html. This is basic stuff, that your Java text book should cover. – JB Nizet Dec 31 '15 at 14:52
  • It still says HashSet cannot be resolved to a type even with `import java.util.HashSet`. – dsaxton Dec 31 '15 at 14:54
  • @JBNizet I'm not using a textbook, just kind of learning haphazardly by web searches and guessing. Any suggestions for good books? – dsaxton Dec 31 '15 at 14:56
  • I've learnt Java 18 years ago, so, not really. The online Java tutorial is not bad, and they're being kept up-to-date. – JB Nizet Dec 31 '15 at 15:01

1 Answers1

3

You are just missing the import statement for the Set class. Every class outside of the java.lang package needs to be explicitely imported.

import java.util.Collections;
import java.util.Set; // <-- needs to import Set to use it

public class DirectedGraph {
    private Set<> vertices;

    // here are some other guesses that also didn't work:
    // private Set<int[]> vertices;
    // private Set<Integer> vertices;
    // private Set<Integer> vertices = new HashSet<Integer>(); // <-- this would need "import java.util.HashSet" to compile
}

All of your guesses are correct. You can't use a primitive type here but you can use int[] (which is not a primitive type, it is an Object that is an array of ints) and Integer (which corresponds the class java.lang.Object).

Community
  • 1
  • 1
Tunaki
  • 132,869
  • 46
  • 340
  • 423