返回ArrayList延迟

时间:2013-08-17 04:19:06

标签: java arraylist

在我的应用程序中,我有一个从另一个类返回的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;
    }

有两个具有相同问题的ArrayLists,我从一个名为PanelDesenho的JPanel填充了paint组件,我在调用数组列表之前调用了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;
}

2 个答案:

答案 0 :(得分:1)

  

我可以在程序中看到更多的前向ArrayList的大小   是正确的,但我想做的一切都已经错了。

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

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

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

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

答案 1 :(得分:0)

我没有找到解决问题的方法,但我找到了一种让程序工作的方法,我在问题中描述的方式它只是不起作用而且我不知道为什么,我只是不得不在程序的其他部分调用函数getCaixas和getCaixasLado,它必须在程序中向前完成,这是让它工作的唯一方法,如果有人知道那里有什么问题请与我们分享,这是一个真的恼人的问题。

相关问题