Java对象的奇怪行为:假设相同的过程,不同的结果

Java对象的奇怪行为:假设相同的过程,不同的结果,java,arrays,object,vector,Java,Arrays,Object,Vector,我在使用Java对象时遇到了一种奇怪的行为。我有两个不同版本的ComponentPlane.class。差异由******标记 第一个工作版本 package app.pathsom.som.output; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JPanel; import app.pathso

我在使用Java对象时遇到了一种奇怪的行为。我有两个不同版本的ComponentPlane.class。差异由
******
标记

第一个工作版本

package app.pathsom.som.output;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;

import app.pathsom.som.map.Lattice;
import app.pathsom.som.map.Node;

public class ComponentPlane extends JPanel{

private Lattice lattice;
private int componentNumber;
private double minValue;
private double maxValue;
private double origMinValue;
private double origMaxValue;

public ComponentPlane(Lattice lattice, int componentNumber){
    this.lattice = new Lattice();
    this.componentNumber = componentNumber;
    initLattice(lattice);
    initComponentPlane();
}

private void initLattice(Lattice lattice){
    this.lattice.setLatticeHeight(lattice.getLatticeHeight());
    this.lattice.setLatticeWidth(lattice.getLatticeWidth());
    this.lattice.setNumberOfNodeElements(lattice.getNumberOfNodeElements());
    this.lattice.initializeValues();
    this.lattice.setNodeHeight(lattice.getNodeHeight());
    this.lattice.setNodeWidth(lattice.getNodeWidth());

    this.lattice.setTotalNumberOfNodes(lattice.getTotalNumberOfNodes());

    for(int i = 0; i < lattice.getTotalNumberOfNodes(); i++){
        ******this.lattice.getLatticeNode()[i] = new Node(lattice.getLatticeNode()[i]);******
    }

}
}
Node.class(第一个工作版本中使用的)中的构造函数是这个

public Node (Node node){
    this.xPos = node.xPos;
    this.yPos = node.yPos;
    this.numOfElements = node.numOfElements;
    this.cluster = -1;
    this.nodeIndex = node.getNodeIndex();
    for(int i = 0; i < this.numOfElements; i++){
        this.addElement(node.getDoubleElementAt(i));
    }
}
公共节点(节点){
this.xPos=node.xPos;
this.yPos=node.yPos;
this.numOfElements=node.numOfElements;
这是1.cluster=-1;
this.nodeIndex=node.getNodeIndex();
对于(int i=0;i
格类

public class Lattice {
private int latticeWidth;
private int latticeHeight;

private int numOfNodeElements;

private int nodeWidth;
private int nodeHeight;
private int totalNumOfNodes;

private Node[] latticeNodes;

private final int MAP_RADIUS = 225;

public Lattice(int latticeWidth, int latticeHeight, int numOfNodeElements){
    this.latticeWidth = latticeWidth;
    this.latticeHeight = latticeHeight;
    this.numOfNodeElements = numOfNodeElements;

    initializeLattice();
}

public Lattice(){
    this(10, 10, 3);
}

public void initializeValues(){
    totalNumOfNodes = this.latticeHeight * this.latticeWidth;
    latticeNodes = new Node[totalNumOfNodes];   //specify the array of nodes

    nodeWidth = (int) Math.floor(450/this.latticeWidth);
    nodeHeight = (int) Math.floor(450/this.latticeHeight);
}

protected void initializeLattice(){
    totalNumOfNodes = this.latticeHeight * this.latticeWidth;
    latticeNodes = new Node[totalNumOfNodes];

    nodeWidth = (int) Math.floor(450/this.latticeWidth);
    nodeHeight = (int) Math.floor(450/this.latticeHeight);

    //initialize colors

    for(int i = 0; i <totalNumOfNodes; i++){
        latticeNodes[i] = new Node(((i % this.latticeWidth) * nodeWidth) + nodeWidth / 2, 
                ((i / this.latticeWidth) * nodeHeight ) + nodeHeight/2, numOfNodeElements, i);
        latticeNodes[i].setNodeColor(new Color((int)(latticeNodes[i].getDoubleElementAt(0) 
                * 255), (int)(latticeNodes[i].getDoubleElementAt(1) * 255), (int) (latticeNodes[i].getDoubleElementAt(2) * 255)));
    }   
}

public int getLatticeHeight(){
    return latticeHeight;
}

public void setLatticeHeight(int latticeHeight){
    this.latticeHeight = latticeHeight;
}

public Node[] getLatticeNode(){
    return latticeNodes;
}

public void setLatticeNode(Node[] latticeNodes){
    this.latticeNodes = latticeNodes;
}

public int getLatticeWidth(){
    return latticeWidth;
}

public void setLatticeWidth(int latticeWidth){
    this.latticeWidth = latticeWidth;
}

public int getNodeHeight(){
    return nodeHeight;
}

public int getNodeWidth(){
    return nodeWidth;
}

public void setNodeHeight(int nodeHeight){
    this.nodeHeight = nodeHeight;
}

public void setNodeWidth(int nodeWidth){
    this.nodeWidth = nodeWidth;
}

public int getNumberOfNodeElements(){
    return numOfNodeElements;
}

public void setNumberOfNodeElements(int numOfNodeElements){
    this.numOfNodeElements = numOfNodeElements;
}

public int getTotalNumberOfNodes(){
    return totalNumOfNodes;
}

public void setTotalNumberOfNodes(int totalNumberOfNodes){
    this.totalNumOfNodes = totalNumberOfNodes;
}
}
公共类晶格{
私有int格子宽度;
私人格子高度;
私有整数元素;
私有节点宽度;
私有节点权重;
私有整数节点;
私有节点[]格子节点;
私有最终整数映射_半径=225;
公共晶格(int-latticeWidth、int-latticeHeight、int-numOfNodeElements){
this.latticeWidth=latticeWidth;
this.latticeHeight=格子高度;
this.numOfNodeElements=numOfNodeElements;
Initializelatice();
}
公共格子(){
这(10,10,3);
}
public void initializeValues(){
totalNumOfNodes=this.latticeHeight*this.latticeWidth;
latticeNodes=新节点[totalNumOfNodes];//指定节点数组
nodeWidth=(int)数学地板(450/这个格子宽度);
节点高度=(int)数学楼层(450/此格子高度);
}
受保护的void initializelatice(){
totalNumOfNodes=this.latticeHeight*this.latticeWidth;
latticeNodes=新节点[totalNumOfNodes];
nodeWidth=(int)数学地板(450/这个格子宽度);
节点高度=(int)数学楼层(450/此格子高度);
//初始化颜色

对于(inti=0;i而言,明显的区别在于,在第一个节点中,您创建了新节点,而在其他节点中,您重用了旧节点

this.lattice.getLatticeNode()[i] = lattice.getLatticeNode()[i];
正在将一个对象设置为另一个对象。如果以后,
lattice.getLatticeNode()[i]
的属性发生更改,这也会影响
此.lattice.getLatticeNode()[i]
。这可能会导致难以找到的错误。相反,行

this.lattice.getLatticeNode()[i] = new Node(lattice.getLatticeNode()[i]);
正在创建一个新对象,与旧对象不同。但是,当然,这意味着您正在使用更多的内存,因为现在您有两个对象而不是一个


您可以做一些事情来减少所使用的内存量。
private final int MAP_RADIUS=225;
可以设置为静态,因此不会为每个节点创建常量的新副本。

TL,DR您需要发布尽可能少的代码问题,以寻求调试帮助(“此代码为什么不工作?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现问题所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建。抱歉,朋友,很少有人愿意通读您的所有输入…@ΦXocę웃Пepeúpaツ 对不起。我解释事情有困难,所以我总是尝试解释得更长。这是我的习惯。对不起that@GhostCat我已经试过编辑这个问题了。谢谢。我道歉。你现在能帮我吗?@ΦXocę웃Пepeúpaツ 我已经试过编辑这个问题了。谢谢。我很抱歉。你现在能帮我吗?谢谢你的回答。有没有办法模仿这个。lattice.getlatticentode()[I]=新节点(lattice.getlatticentode()[I]);通过使用这个。lattice.getlatticentode()[I]=lattice.getlatticentode()[I];或者通过不创建另一个节点?实际上,正是由于这个原因,我得到了outofmemory错误。我可能创建了太多的节点。每当我每个节点有700多个权重/元素和289个节点时,它就会开始滞后。因此,在ComponentPlane数组大小289上创建了700 x 289=202390个节点。如果您希望请让我只使用实例化新节点()的节点基本上,我正在创建一个热图数组。创建它们之后,我再次进入所有组件平面的循环,然后将它们放入一个JPanel。如果你不创建新节点,节点将被绑定在一起,所有热图都将是相同的-如果你想让它们分开,它们必须是分开的对象。我会继续工作使
节点
类尽可能紧凑。如果在
节点
中有一个类级别的
double
变量,请查看是否可以将其改为
float
。如果有
int
,请查看它是否可以是
short
甚至是
byte
。您可能需要进行一些转换,但它可能值得这是因为记忆对你来说是个问题。
public void initComponentPlanes(){
    componentPlanes = new ComponentPlane[somtrainer.getLattice().getNumberOfNodeElements()];
    int size = somtrainer.getLattice().getNumberOfNodeElements();
    for(int i = 0; i < size; i++){
        System.out.println(i + ": " + inputData.getVariableLabels()[i] + " size : " + size);
        componentPlanes[i] = new ComponentPlane(somtrainer.getLattice(), i);
        componentPlanes[i].setBounds((240 - 225)/2, (280-240)/2, 225, 240);
        componentPlanes[i].setOrigMaxMin(maxMin[i][0], maxMin[i][1]);
    }
}
this.lattice.getLatticeNode()[i] = lattice.getLatticeNode()[i];
this.lattice.getLatticeNode()[i] = new Node(lattice.getLatticeNode()[i]);