I decided to learn a little more about TreeMap and I am having some issues and was hoping for some advice.
package org.search.BFS; import java.util.Random; import java.util.TreeMap; public class Graph { protected class node { int number; boolean visited; } protected TreeMap<node,Integer> tree = new TreeMap<node,Integer>(); Graph() { } Graph(node s, int v) //constructor { tree.put(s,v); } void fillTree() { //fills the graph with 100 random numbers Random random = new Random(); //create a new node in method node n = new node(); for(int i=0;i<=100;i++) { n.number=random.nextInt(100); //add a random number to the node n.visited = false;//set to false inside node n=new node(); //create new node tree.put(n, i);//place node into tree } } void printTree() { int i =0; while(!tree.isEmpty()) { System.out.println(tree.get(i)); i++; } } public static void main(String [] args) { Graph graph = new Graph(); graph.fillTree(); graph.printTree(); } }
errors: Exception in thread "main" java.lang.ClassCastException: org.search.BFS.Graph$node cannot be cast to java.lang.Comparable at java.util.TreeMap.compare(TreeMap.java:1188) at java.util.TreeMap.put(TreeMap.java:531) at org.search.BFS.Graph.fillTree(Graph.java:37) at org.search.BFS.Graph.main(Graph.java:56)