0

在我的应用程序中,我有一个从另一个类返回的 ArrayList,在下一行中,我需要获取它的大小以创建另一个数组,我将在其中放置一些地址,并且它始终返回大小 0,在调试模式下,我可以看到程序中更多的 ArrayList 的大小是正确的,但是我想做的一切已经错了,我尝试使用 Thread.sleep(10000) 和 10000 空白循环,仍然没有,请有人帮忙,对不起我的英语,这里有一点代码:

    caixas = campo.getCaixas(); //this is where i return the ArrayList
    enderecosM = new byte[caixas.size()][2];
    for (int j = 0; j < caixas.size(); j++) {
        enderecosM[j][0]=0;
        enderecosM[j][1]=0;
    }

有 2 个 ArrayLists 有同样的问题,我让它们从一个名为 PanelDesenho 的 JPanel 中填充了绘制组件,我在调用数组列表之前调用了 repaint 方法。

private void desenha() {
    nLinhas = (int) Integer.valueOf(textLinhas.getText());
    nColunas = (int) Integer.valueOf(textColunas.getText());
    total = nLinhas * nColunas;
       //pass the parameters to create the ArrayLists.
    campo.defParametros(nLinhas, nColunas, matrizList);
    panelDesenho.repaint();
    panelDesenho.revalidate();
    sensores = campo.getSensores();
    caixas = campo.getCaixas();
    caixasLado = campo.getCaixasLado();
    enderecosM = new byte[caixas.size()][2];
    for (int j = 0; j < caixas.size(); j++) {
        enderecosM[j][0]=0;
        enderecosM[j][1]=0;
    }
    enderecosL = new byte[caixasLado.size()][2];
    for (int j = 0; j < caixas.size(); j++) {
        enderecosL[j][0]=0;
        enderecosL[j][1]=0;
    }
}

现在有 PaintComponent 方法:

protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            campo.desMatriz(g2d);
        }

以及我填充 ArrayLists 的方法中的代码:

public void defParametros(int lin, int col, ArrayList<Integer> arr) {
    linhas = lin;
    colunas = col;
    arrList = arr;
}



public void desMatriz(Graphics2D g2d) {
    int i, n, k = 0, z=0, y=0;
    sensores.clear();
    caixas.clear();
    caixasLado.clear();
    for (n = 0; n < linhas; n++) {
        for (i = 0; i < colunas; i++) {
            if (i%2==0 && i<colunas-1){
                caixas.add(new Rectangle2D.Double(145+50*i, 10+100*n, 30, 20));
                g2d.fill(caixas.get(z));
                z++;
            }
            if (n%2==0 && n<linhas-1){
                if (i==0){
                    caixasLado.add(new Rectangle2D.Double(30, 155+100*n, 20, 30));
                    g2d.fill(caixasLado.get(y));
                    y++;
                }
                if (i==colunas-1){
                    caixasLado.add(new Rectangle2D.Double(170+50*i, 155+100*n, 20, 30));
                    g2d.fill(caixasLado.get(y));
                    y++;
                }
            }
            sensores.add(new Ellipse2D.Double(100 + 50 * i, 60 + 100 * n, 20, 20));
            g2d.fill(sensores.get(k));
            k++;
        }
    }
}

最后,我返回 ArrayLists:

public ArrayList<Rectangle2D> getCaixas(){
    return caixas;
}

public ArrayList<Rectangle2D> getCaixasLado(){
    return caixasLado;
}
4

2 回答 2

1

我可以在程序中看到更多的 ArrayList 的大小是正确的,但是我想做的所有事情都已经错了。

您似乎正在处理一个共享列表,该列表会被当前班级或其他班级中的某些语句修改。因此,您会在执行时看到该列表未填写:

caixas = campo.getCaixas(); //this is where i return the ArrayList

但由于执行了其他一些代码,它稍后会被填充。

您需要找到填充列表的代码,如果需要,您可以在上述语句之前执行该代码。

于 2013-08-17T04:22:23.190 回答
0

我没有找到解决问题本身的方法,但我找到了使程序工作的方法,我在问题中描述的方式它只是不起作用,我不知道为什么,我只需要调用函数getCaixas 和 getCaixasLado 在程序的其他部分,它必须在程序中完成,这是使它工作的唯一方法,如果有人知道那里有什么问题,请与我们分享,这是一个非常烦人的问题。

于 2013-08-19T12:14:36.960 回答