그래프 (GraphImp) 개체와 노드 (NodeImp) 개체가있는 할당에 대한 그래프 구현을 만들려고합니다.LinkedList : java.lang.OutOfMemoryError : Java 힙 공간
노드 객체에는 해당 그래프에 대한 참조, x 좌표 & y 좌표 및 이름이 포함됩니다.
그래프 개체는 해당 노드의 연결된 목록을 포함합니다.
노드 목록 중간에 노드를 추가하려고하면 문제가 발생합니다 (끝에 추가하는 것이 좋습니다). 프로그램의 힙 공간이 부족합니다. 나는 LinkedList에 삽입하는 복잡성이 O (1)이어야하고 Java (나는 믿는다)가 객체를 사용하는 대신 포인터를 사용하기 때문에 이것이 왜 발생하는지 확신 할 수 없다. 또한 arraylist를 시도했습니다.
힙을 더 크게 만드는 것은이 경우에는 옵션이 아니며 문제의 원인이되어서는 안됩니다.
미리 감사드립니다. 여기
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.LinkedList.addBefore(LinkedList.java:795)
at java.util.LinkedList.add(LinkedList.java:361)
at pt.graph.GraphImp.addNode(GraphImp.java:79)
at pt.graph.NodeImp.<init>(NodeImp.java:25)
at pt.graph.Graphs.newNode(Solution.java:68)
는 코드입니다 :
class Graphs
{
static Node newNode(Graph g, double xpos, double ypos, String name) throws InvalidGraphException,InvalidLabelException
{
if(g==null || !(g instanceof GraphImp)){ //Checking validity of inputs
throw new InvalidGraphException();
}
if(name==null){
throw new InvalidLabelException();
}
NodeImp[] existNodes = ((GraphImp)g).getNodes(); //Get all Nodes already present in the Graph
for(int i=0;i<existNodes.length;i++){
if(existNodes[i].getXPos() == xpos && existNodes[i].getYPos() == ypos){ //If node already present at this position, throw InvalidLabelException()
throw new InvalidLabelException();
}
}
Node n = new NodeImp((GraphImp)g, xpos, ypos, name); //If all inputs are valid, create new node
return n;
}
}
class NodeImp extends Node //Node Class
{
private Object flags = null;
private GraphImp g = null;
private double xpos = 0.0;
private double ypos = 0.0;
private String name = "";
NodeImp(GraphImp g, double xpos, double ypos, String name){
this.g = g;
this.xpos = xpos;
this.ypos = ypos;
this.name = name;
g.addNode(this); // Add Node to the Graph
}
}
class GraphImp extends Graph
{
private LinkedList<NodeImp> nodes = new LinkedList<NodeImp>(); //LinkedList of all Nodes in the Graph
GraphImp(){
}
NodeImp[] getNodes(){ //Returns an array of all Nodes
NodeImp[] nArr = new NodeImp[nodes.size()];
return nodes.toArray(nArr);
}
int countNodes(){ //Returns number of Nodes
return nodes.size();
}
void addNode(NodeImp n){ //Add a Node to the LinkedList in order
boolean added = false;
for(int i = 0;i<nodes.size();i++){
if(n.compareTo(nodes.get(i))<=0){
nodes.add(i,n); //fails here
}
}
if(!added){
nodes.add(n);
}
return;
}
}
Java에서 포인터에 대한 명시적인 개념은 없습니다. 모든 것이 대상입니다. – noMAD
여기서 Node 클래스의 코드는 무엇입니까? –
@noMAD : 예, 아니오. 변수는 객체에 대한 모든 참조 *입니다. 객체 자체는 아닙니다. 참조는 포인터와 비슷하게 (그러나 동일하지 않게) 동작합니다. –