# unchecked conversion found

Printable View

• 08-12-2011, 03:24 AM
Jeffrod
unchecked conversion found
Doing a school assignment and cant figure out how the get rid of 2 unchecked conversions.
Everything works as it should but i figured id sort this before handing the assignment in.
Code:

```        private static<E> void depthFirstSearch(Graph graph, E e, Set<E> visited){                 visited.add(e);                 Map<E, List<ListEdge<E>>> nodes = graph.getNodes();                 for(ListEdge<E> le : nodes.get(e)){                         if(!visited.contains(le.getDest())){                                 depthFirstSearch(graph, le.getDest(), visited);                         }                 }         }```
Code:

```        public static<E extends Comparable<? super E>> List<ListEdge<E>> shortestPath(Graph graph, E from, E to){                 if(pathExists(graph, from, to)){                         if(from.compareTo(to)==0)                                 return null;                         Map<E, List<ListEdge<E>>> nodes = graph.getNodes();```
the second method is very long so only took the first part of it.
All the extends and supers are attempts to sort it.
If it matter the class is decalred like this:
Code:

`public class GraphMethods<E extends Comparable<? super E>>{`
And the method that is causing all the troubles:
Code:

```        public Map<E,List<ListEdge<E>>> getNodes(){                 Map<E,List<ListEdge<E>>> temp = nodes;                 return temp;         }```
Code:

```public class ListGraph<E extends Comparable<? super E>> implements Graph<E>{         private Map<E,List<ListEdge<E>>> nodes = new TreeMap<E, List<ListEdge<E>>>();```
both warnings are "unchecked conversion found" and points to getNodes() in the first 2 code examples
• 08-12-2011, 03:28 AM
Norm
What are the definitions of the getNodes() method and the Graph class?
• 08-12-2011, 01:24 PM
Jeffrod
Graph is just a interface declared like:
Code:

`public interface Graph<E extends Comparable<? super E>>`
i posted the entire getNodes() method in my earlier post. It basically just returns a copy of the whole data structure which is just a TreeMap.
Code:

`private Map<E,List<ListEdge<E>>> nodes = new TreeMap<E, List<ListEdge<E>>>();`
in my desperation i tried something like this:
Code:

```Object obj = graph.getNodes(); Map<E, List<ListEdge<E>>> nodes = (Map<E, List<ListEdge<E>>>)obj;```
but that ofc just left me with a unchecked cast warning.
• 08-12-2011, 04:40 PM
Norm
Can you make a complete program that demonstrates the problem. Something that compiles and that could be worked on.
• 08-14-2011, 04:05 AM
Jeffrod
well thanks for trying but a friend explained it to me and it was, as usual, very simple and as ive tried about a dozen different things i had actually almost sorted it before :(blush):

how to sort it:
Code:

`public static<E extends Comparable<? super E>> List<ListEdge<E>> shortestPath(Graph<E> graph, E from, E to){`
as opposed to the old one that gave a warning:
Code:

``` public static<E extends Comparable<? super E>> List<ListEdge<E>> shortestPath(Graph graph, E from, E to){```
and if you cant spot it its the Graph<E> graph instead of just Graph graph. Silly little thing but ive found that its the silly little thing that takes most of my time