简体   繁体   English

如何在Java中使用TreeMap在.get()和.put()中使用字符串

[英]How to use Strings for .get() and .put() with TreeMap in Java

I am currently working on creating a Prim's Minimum Spanning Tree for an undirected, weighted graph that uses String values for the vertices. 我目前正在为使用String值作为顶点的无向加权图创建Prim的最小生成树。 To create the graph, my teacher said we can use the edge and graph classes from our textbook. 我的老师说,要创建图形,我们可以使用课本中的edge和graph类。 However, the book uses Integers for vertices rather than strings. 但是,本书使用Integers作为顶点而不是字符串。 I tried replacing all Integers with Strings, but I received a compiler error for every line that used .get() from the generic TreeMap because it could not find the symbol method get(java.lang.String). 我尝试将所有Integer替换为Strings,但是由于从通用TreeMap中使用.get()的每一行都收到了编译器错误,因为它找不到符号方法get(java.lang.String)。 After a little work, I discovered that initializing the TreeMap and using .add() works with Strings, but not the .get() or .put() methods. 经过一些工作,我发现初始化TreeMap并使用.add()可用于字符串,但不能用于.get()或.put()方法。 Here is the code exactly the way it is in the book except Integer is replaced with String. 此处的代码与书中的代码完全相同,只是将Integer替换为String。

How can I make the .get() and .put() methods to work with Strings? 如何使.get()和.put()方法与字符串一起使用?

import java.util.*;

class Graph {
    private int numVertices;    //number of vertices in the graph
    private int numEdges;        //number of edges in the graph

    private Vector<TreeMap<String, String>> adjList;

    //constructor
    public Graph(int n) {
        numVertices=n;
        numEdges=0;
        adjList=new Vector<TreeMap<String, String>>();
        for(int i=0;i<numVertices;i++) {
            adjList.add(new TreeMap<String, String>());
        }
    }

    //Determines the number of vertices in the graph
    public int getNumVertices() {
        return numVertices;
    }

    //Determines the number of edges in the graph
    public int getNumEdges() {
        return numEdges;
    }

    //Determines the weight of the edge between vertices v and w
    public String getEdgeWeight(String v, String w) {
        return adjList.get(v).get(w);
    }

    //Add the edge to both v's and w's adjacency list
    public void addEdge(String v, String w, int wgt) {
        adjList.get(v).put(w,wgt);
        adjList.get(w).put(v,wgt);
        numEdges++;
    }

    //Adds an edge to the graph
    public void addEdge(Edge e) {
        //Extract the vertices and weight from the edge e
        String v=e.getV();
        String w=e.getW();
        int weight=e.getWeight();
        addEdge(v, w, weight);
    }

    //Finds the edge connecting v and w
    public Edge findEdge(String v,String w) {
        int wgt=adjList.get(v).get(w);
        return new Edge(v, w, wgt);
    }

    //package access
    //Returns the adjacency list for given vertex
    TreeMap<String, String> getAdjList(String v) {
        return adjList.get(v);
    }
}

Your problem is not with the TreeMap . 您的问题不在于TreeMap TreeMap is key based collection. TreeMap是基于密钥的集合。 You are facing issues with your Vector ' adjList '. 您的adjList '面临问题。 Vector is an Indexed based collection you can get an item with its index only. Vector是基于索引的集合,您只能获得带有其索引的项目。

Try changing your method as below 尝试如下更改方法

public String getEdgeWeight(int v, String w) {
    return adjList.get(v).get(w);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM