0
public class Tabel {
    private static int dimension;

    private ArrayList<ArrayList<Character>> tabel;


    public Tabel(int dimension) {

        Tabel.dimension = dimension;

        for (int i=0;i<Tabel.dimension*Tabel.dimension;i++) {
           tabel.add(new ArrayList<Character>());
        }

     }
}

デバッグ (Eclipse ide) しようとすると、多くの奇妙な「エラー」が発生するか、少なくとも予期しないものに遭遇します。

private static int は、デバッグの「変数」セクションには表示されません。

NullPointerExceptionオンになりますtabel.add(...)が、デバッグを監視すると、for一度入力され、テーブルに何も追加されません。これは、右中括弧にジャンプする代わりに「次へ」を押すと、関数からジャンプするためです。

コメントする.addとうまくいくので、それが問題です(私は思います)。私の構文は間違っていますか?または、さらにコードを投稿する必要がありますか?

4

3 に答える 3

5

tabel初期化されていないため、null です。

変化する

private ArrayList<ArrayList<Character>> tabel;

private ArrayList<ArrayList<Character>> tabel = new ArrayList<ArrayList<Character>>();

またはより良い:

private List<ArrayList<Character>> tabel = new ArrayList<ArrayList<Character>>();

これは に結びつかないtabelからArrayListです。

于 2013-05-21T17:18:15.223 に答える
1

privateリストを初期化していません。以下をせよ:

private List<ArrayList<Character>> tabel = new ArrayList<ArrayList<Character>>();
于 2013-05-21T17:18:44.053 に答える
0

I'd have trouble understanding that level of nesting too.

It's better to refer to List rather than ArrayList. Unless you need a method in the concrete class, it makes your program more flexible to refer to the interface and the methods in the interface.

Create a class (1) that has a field defined as a List of Character. Set the field to a new ArrayList in the constructor.

Create another class (2) that has a field defined as a List of class (1). Set the field to a new ArrayList in the constructor.

Create another class (3) that has a field defined as a List of class (2). Set the field to a new ArrayList in the constructor.

Since you understand what you're doing, you can give these 3 classes more meaningful names.

于 2013-05-21T18:06:00.197 に答える