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