Results 1 to 5 of 5
  1. #1
    Jeffrod is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default 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.
    Java 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);
    			}
    		}
    	}
    Java 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:
    Java Code:
    public class GraphMethods<E extends Comparable<? super E>>{
    And the method that is causing all the troubles:
    Java Code:
    	public Map<E,List<ListEdge<E>>> getNodes(){
    		Map<E,List<ListEdge<E>>> temp = nodes;
    		return temp;
    	}
    Java 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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    What are the definitions of the getNodes() method and the Graph class?

  3. #3
    Jeffrod is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Graph is just a interface declared like:
    Java 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.
    Java Code:
    private Map<E,List<ListEdge<E>>> nodes = new TreeMap<E, List<ListEdge<E>>>();
    in my desperation i tried something like this:
    Java 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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    Can you make a complete program that demonstrates the problem. Something that compiles and that could be worked on.

  5. #5
    Jeffrod is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    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

    how to sort it:
    Java 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:
    Java 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

Similar Threads

  1. Replies: 9
    Last Post: 05-11-2011, 04:31 PM
  2. Compile error - unchecked
    By pahiker in forum New To Java
    Replies: 18
    Last Post: 06-24-2010, 09:27 AM
  3. unchecked exceptions
    By veens4444 in forum New To Java
    Replies: 1
    Last Post: 06-08-2010, 06:36 AM
  4. Uses unchecked or unsafe operations message
    By Robbinz in forum New To Java
    Replies: 2
    Last Post: 12-06-2007, 10:56 PM
  5. UnChecked Exception
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-18-2007, 07:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •