私のアプリケーションでは、別のクラスから返された 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;
}
同じ問題を抱えた ArrayList が 2 つあります。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;
}