Java将问题呈现以填充HashMap。

huangapple go评论72阅读模式
英文:

Java presenting problem to fill the HashMap

问题

以下是翻译好的部分:

下面的代码在填充 HashMap 时呈现出奇怪的行为。可以看到有一个 for 循环,它遍历一个 Faces 列表,并将一个 IntegrationCellVertex 添加到每个 Face 的名为 values 的映射中。然而,在第一个循环中,它将此对象添加到了列表中的多个 Face 中(见图像中蓝色高亮部分)。是否有人能够想出可能发生了什么?不幸的是,代码太大,所以我只分享出现问题的部分。

int vertexCounter = 0;
for (Face face : this.getDomainDataStructure().getFacesList()) {
    IPolygon polygon = face.faceToPolygon();
    IPoint3d centroid = polygon.getCentroid();
    IntegrationCellVertex integrationCellVertex = new IntegrationCellVertex();
    integrationCellVertex.setPoint(centroid);
    vertexCounter += 1;
    integrationCellVertex.setLabel(String.valueOf(vertexCounter));
    integrationCellVertex.setContainingFace(face);
    face.setValue(MeshfreeModel.INTEGRATION_CELL_VERTEX, integrationCellVertex);
    this.getModel().addIntegrationCellsVertex(integrationCellVertex);
}

Face 类:

public class Face implements Serializable {
    // ...(省略了类的其余定义和方法)
}

Java将问题呈现以填充HashMap。

英文:

The code below is presenting a strange behavior when filling a HashMap. As can be seen there is a for that goes through a list of Faces and adds one IntegrationCellVertex into the map called values of each Face. However, in the first loop it adds this object into more than one Face of the list (see the image, highlighted in blue). Would anyone have any idea what may be going on? Unfortunately, the code is too big, so I just sharing the section where is happening the problem.
Thanks a lot!

int vertexCounter = 0;
for (Face face : this.getDomainDataStructure().getFacesList()) {
    IPolygon polygon = face.faceToPolygon();
    IPoint3d centroid = polygon.getCentroid();
    IntegrationCellVertex integrationCellVertex = new IntegrationCellVertex();
    integrationCellVertex.setPoint(centroid);
    vertexCounter += 1;
    integrationCellVertex.setLabel(String.valueOf(vertexCounter));
    integrationCellVertex.setContainingFace(face);
    face.setValue(MeshfreeModel.INTEGRATION_CELL_VERTEX, integrationCellVertex);
    this.getModel().addIntegrationCellsVertex(integrationCellVertex);
}

Face class:

public class Face implements Serializable {
private static final long serialVersionUID = 1L;
private LinkedList<Loop> loopList = new LinkedList<Loop>();
private HashMap<String, Object> values;
private ArrayList<String> keys;
private Solid solid;
private String id;
private LinkedList<Vertex> internalVertices = new LinkedList<Vertex>();
private ArrayList<Face> facesVizinhas = new ArrayList<Face>();
private ArrayList<Edge> oldEdgesVizinhas = new ArrayList<Edge>();
private ArrayList<Edge> newEdgesVizinhas = new ArrayList<Edge>();
private ArrayList<Edge> oldOldEdgesVizinhas = new ArrayList<Edge>();
/**
* The constructor of this class.
*/
public Face() {
this.values = new HashMap<String, Object>();
this.keys = new ArrayList<String>();
}
/**
* The constructor of this class.
* 
* @param id The identification of this face.
* @param loop The loop.
*/
public Face(String id, Loop loop) {
this.id = id;
this.loopList.add(loop);
loop.setFace(this);
this.values = new HashMap<String, Object>();
this.keys = new ArrayList<String>();
}
/**
* getPlanarSubdivision.
* @return Returns the planarSubdivision.
*/
public Solid getSolid() {
return solid;
}
/**
* setPlanarSubdivision.
* @param planarSubdivision The planarSubdivision to set.
*/
public void setSolid(Solid planarSubdivision) {
this.solid = planarSubdivision;
}
/**
* getId.
* @return Returns the index.
*/
public String getId() {
return id;
}
/**
* setIndex.
* @param id The index to set.
*/
public void setId(String id) {
this.id = id;
}
/**
* getLoopList.
* @return Returns the loopList.
*/
public LinkedList<Loop> getLoopList() {
return loopList;
}
/**
* setLoopList.
* @param loopList The loopList to set.
*/
public void setLoopList(LinkedList<Loop> loopList) {
this.loopList = loopList;
}
/**
* Returns a value of this face.
* 
* @param key The key of the value.
* @return The face value.
*/
public Object getValue(String key) {
return this.values.get(key);
}
/**
* The method set a value at a key of the face values.
* 
* @param key The key of the value.
* @param value The value.
*/
public void setValue(String key, Object value) {
if (!this.keys.contains(key)) {
this.keys.add(key);
}
this.values.put(key, value);
}
/**
* Returns true if the node has the point values for the key.
* 
* @param key The key of the point values.
* @return True if the node has the point values for the key.
*/
public boolean valuesContainsKey(String key) {
return this.values.containsKey(key);
}
/**
* The method return the values.
* 
* @return Returns The values.
*/
public HashMap<String, Object> getValues() {
return this.values;
}
/**
* Returns the keys of this face.
* 
* @return The keys of this face.
*/
public ArrayList<String> getKeys() {
return keys;
}
/**
* setKeys.
* @param keys The keys to set.
*/
public void setKeys(ArrayList<String> keys) {
this.keys = keys;
}
/**
* The method sets the HashMap.
* 
* @param values the values to set
*/
public void setValues(HashMap<String, Object> values) {
this.values = values;
}
/**
* Returns true if the face contains the vertex.
* 
* @param vertex The vertex
* @return If the face contains the vertex
*/
public boolean contains(Vertex vertex) {
boolean contains = false;
for (int i = 0; i < this.loopList.size(); i++) {
for (int j = 0; j < this.loopList.get(i).getHalfEdgeList().size(); j++) {
if (vertex.equals(this.loopList.get(i).getHalfEdgeList().get(j).getVertex())) {
contains = true;
break;
}
}
}
return contains;
}
/**
* Returns the internalVertices.
* @return the internalVertices
*/
public LinkedList<Vertex> getInternalVertices() {
return internalVertices;
}
public ArrayList<Face> getFacesVizinhas() {
return facesVizinhas;
}
public void setFacesVizinhas(ArrayList<Face> facesVizinhas) {
this.facesVizinhas = facesVizinhas;
}
public ArrayList<Edge> getOldEdgesVizinhas() {
return oldEdgesVizinhas;
}
public void setOldEdgesVizinhas(ArrayList<Edge> oldEdgesVizinhas) {
this.oldEdgesVizinhas = oldEdgesVizinhas;
}
public ArrayList<Edge> getNewEdgesVizinhas() {
return newEdgesVizinhas;
}
public void setNewEdgesVizinhas(ArrayList<Edge> newEdgesVizinhas) {
this.newEdgesVizinhas = newEdgesVizinhas;
}
public ArrayList<Edge> getOldOldEdgesVizinhas() {
return oldOldEdgesVizinhas;
}
public void setOldOldEdgesVizinhas(ArrayList<Edge> oldOldEdgesVizinhas) {
this.oldOldEdgesVizinhas = oldOldEdgesVizinhas;
}
/**
* Returns the two edges of this face that are connected to the informed vertex.
* @param vertex a vertex
* @return the two edges connected to this vertex
*/
public Edge[] getEdges(Vertex vertex) {
// Initialization of the edges array
Edge[] edges = new Edge[2];
// Auxiliary variables
Edge edge;
HalfEdge halfEdge;
int counter = 0;
// Iteration over the loops of this face
ListIterator<Loop> loops = this.getLoopList().listIterator();
while (loops.hasNext()) {
// This loop
Loop loop = loops.next();
// Iteration over the half-edges of this loop
ListIterator<HalfEdge> halfEdges = loop.getHalfEdgeList().listIterator();
while (halfEdges.hasNext()) {
// This half-edge
halfEdge = halfEdges.next();
// The edge of this half-edge
edge = halfEdge.getEdge();
// Check if this edge contains the vertex
if (edge.getRightHalfEdge().getVertex().isTheSame(vertex) || edge.getLeftHalfEdge().getVertex().isTheSame(vertex)) {
edges
0
+
网站访问量
= edge; counter++; } } } return edges; } /** * Returns the vertices associated to this face. * @return an {@link ArrayList} of instances of the class {@link Vertex} */ public ArrayList<Vertex> getVertices() { ArrayList<Vertex> vertices = new ArrayList<>(); ListIterator<Loop> loops = this.loopList.listIterator(); while (loops.hasNext()) { Loop loop = loops.next(); ListIterator<HalfEdge> halfEdges = loop.getHalfEdgeList().listIterator(); while (halfEdges.hasNext()) { HalfEdge halfEdge = halfEdges.next(); Vertex vertex = halfEdge.getVertex(); vertices.add(vertex); } } return vertices; } /** * Returns an instance of the class {@link IPolygon} constructed using the vertices of this face. * @return an instance of the class {@link IPolygon} */ public IPolygon faceToPolygon() { ArrayList<IPoint3d> points = new ArrayList<>(); ListIterator<Vertex> vertices = this.getVertices().listIterator(); while (vertices.hasNext()) { Vertex vertex = vertices.next(); points.add(vertex.getCoords()); } return new IPolygon(points); } }

Java将问题呈现以填充HashMap。

答案1

得分: 1

看看你的代码,与你的描述明显不符。

你说:

> 可以看到有一个循环遍历一个Faces列表,并将一个IntegrationCellVertex添加到每个Face的名为values的映射中。

然而唯一一个添加任何积分单元顶点到任何地方的地方是

> this.getModel().addIntegrationCellsVertex(integrationCellVertex);

很明显是添加到了this.getModel(),而不是face

你的代码中没有包含任何put调用到任何映射中,而且你的图片展示了相同的值在多个face的values映射中。基本上,你贴出的代码与你的问题无关,也许贴出更多代码,或者创建一个自包含的测试案例。你的代码中可能存在更多问题,不仅仅是它执行的操作与你理解的操作方式不一致。

如果你不能创建一个自包含的测试案例,face.setValuemodel.addIntegrationCellsVertex 看起来都是必需的源代码,需要添加到你的问题中,以便任何人能够为你的问题提供一些解答。

英文:

Well, look at your code, which is in clear disagreement with your description.

You say:

> As can be seen there is a for that goes through a list of Faces and adds one IntegrationCellVertex into the map called values of each Face.

and yet the one and only place that adds any integration cells vertex to anything is

> this.getModel().addIntegrationCellsVertex(integrationCellVertex);

which is clearly adding it to this.getModel(), not face.

Your code does not include any put calls into any maps, and your picture is showing off how the same value is in the values map of multiple faces. Basically, the code you pasted is irrelevant to your question, perhaps paste more, or make a self contained test case. There may well be more problems with your code than just the disconnect between what it does and how you understand it.

If you can't make a self contained test case, face.setValue, and model.addIntegrationCellsVertex all look like required source code that needs to be added to your question in order for anybody to shed some light on your issue.

答案2

得分: 1

Second answer now that you've updated your code:

Your code cannot result in the debug state you've shown, unless someone did something stupid, like, say:

Face a = this.getDomainDataStructure().getFacesList().get(0);
Face b = this.getDomainDataStructure().getFacesList().get(1);
a.setValues(b.getValues());

at which point the values map of 'a' and the values map of 'b' are just pointers to the same map. Modify one, and the other one is also modified (if you have a street address written on a piece of paper, and I have an entirely different piece of paper with the exact same address written on it, and you follow your paper and toss a brick through the window, and later I drive over there, I'll find a vandalized house. Same situation).

Take your Face class. Delete all the setters. Make all fields final.

Now, all errors? Those are the actual places that are buggy or at least sufficiently silly that other code (such as the one you pasted) will then fail. Fix THOSE. The problem is there, not in what you pasted.

英文:

Second answer now that you've updated your code:

Your code cannot result in the debug state you've shown, unless someone did something stupid, like, say:

Face a = this.getDomainDataStructure().getFacesList().get(0);
Face b = this.getDomainDataStructure().getFacesList().get(1);
a.setValues(b.getValues());

at which point a's values map and b's values map are just pointers to the same map. modify one, and the other one is also modified (if you a street address written on a piece of paper, and I have another entirely different piece of paper with the exact same address written on it, and you follow your paper and toss a brick through the window, and later I drive over there, I'll find a vandalized house. Same situation).

Take your Face class. Delete all the setters. Make all fields final.

Now, all errors? Those are the actual places that are buggy or at least sufficiently silly that other code (such as the one you pasted) will then fail. Fix THOSE. The problem is there, not in what you pasted.

huangapple
  • 本文由 发表于 2020年9月29日 06:39:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64110527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定