我定義了Map revmap=new HashMap();
但是當我向revmap中添加數據時總提示錯誤。
revmap.put(num_vertices, p);
其中num_vertices是int型,p是Point2d型,錯誤提示讓把num_vertices改為integer型是怎麼回事?
補充內容:
其實我是看到了一個C++程序,需要把它該寫為java程序,C++代碼所用的數據結構如下:
std::map<int, int> Map;
std::map<int, Point2d> revmap;
std::vector<std::vector<Point2d> > components;
還有
typedef std::vector< std::set<int> > graph_t;
其中Point2d是這樣一個結構:
struct Point2d {
int x;
int y;
float SWT;
};
這幾個我不知道用java怎麼實現,就是我嘗試用
Map<integer, integer> map=new HashMap<integer, integer>();
實現
std::map<int, int> Map;
時,在map中put兩個int時就會報錯。
誰知道C++的這種類型對應Java中應該怎麼實現?
JSP為:
<%@ page contentType="text/html" language="java" errorPage="" pageEncoding="GBK"%>
<%@ page import="tsinggao.Point2d"%>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.Set" %>
<%@ page import="java.util.Iterator" %>
<html>
<head>
<title>JSP中添加Map並顯示
</title>
</head>
<body>
<%
Map<Integer,Point2d> myMap = new HashMap<Integer,Point2d>();
Point2d myPoint2d1 = new Point2d();
Point2d myPoint2d2 = new Point2d();
Point2d myPoint2d3 = new Point2d();
myPoint2d1.setX(100);
myPoint2d1.setY(100);
myPoint2d1.setSWT(1000/10);
myMap.put(1, myPoint2d1);
myPoint2d2.setX(200);
myPoint2d2.setY(200);
myPoint2d2.setSWT(2000/10);
myMap.put(2, myPoint2d2);
myPoint2d3.setX(300);
myPoint2d3.setY(300);
myPoint2d3.setSWT(3000/10);
myMap.put(3, myPoint2d3);
Set<Integer> mykeySet = myMap.keySet();
int i=1;
for (Iterator<Integer> iterator = mykeySet.iterator(); iterator.hasNext();) {
Integer myInteger = iterator.next();
Point2d resultPoint2d = myMap.get(myInteger);
%>
Map中,第<%=String.valueOf(i) %>個元素的Key為:<%=String.valueOf(myInteger) %><br>
第<%=String.valueOf(i) %>個元素的Value為:
x=<%=String.valueOf(resultPoint2d.getX()) %>;
y=<%=String.valueOf(resultPoint2d.getY()) %>;
float=<%=String.valueOf(resultPoint2d.getSWT()) %><br><br>
<%
i++;
}
%>
<%%>
</body>
</html>
Point2d 類文件為:tsinggao.Point2d.java
package tsinggao;
public class Point2d {
int x;
int y;
float SWT;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public float getSWT() {
return SWT;
}
public void setSWT(float swt) {
SWT = swt;
}
};