gpt4 book ai didi

java - ArrayList延迟返回

转载 作者:行者123 更新时间:2023-12-01 14:13:54 25 4
gpt4 key购买 nike

在我的应用程序中,我有一个从另一个类返回的 ArrayList,在下一行中,我需要获取它的大小以创建另一个数组,我将在其中放置一些地址,并且它总是返回size 0,在 Debug模式下我可以看到程序中的更多内容 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 个 ArrayList 存在同样的问题,我让它们从名为 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);
}

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

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;
}

最佳答案

i could see that more forward in the program the size of the ArrayList is correct, but there all that i wanted to do is already wrong.

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

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

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

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

关于java - ArrayList延迟返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18285054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com