简体   繁体   中英

Cannot find symbol error for variables My2DTableCoordinate and Arraylist?

Arraylist is imported at top of class like:

import java.util.ArrayList

however it still errors from the code

coordinates.add(ArrayList<My2DTableCoordinate>(height));

which is taken from a method called

public My2DTable(int height) {
coordinates = new ArrayList<ArrayList<My2DTableCoordinate>>(height);
for(int i = 0; i < height; i++) {
  coordinates.add(ArrayList<My2DTableCoordinate>(height));
}

the 2d arraylist is declared as

ArrayList<ArrayList<My2DTableCoordinate>> coordinates;

thanks to @ycf_l

There's a lot missing here. Where is coordinates defined? Also ArrayList<My2DTableCoordinate>(height) should be new ArrayList<My2DTableCoordinate>(height) .

It would help a great deal if we knew exactly what error you're getting.

您需要创建类My2DTableCoordinate或导入它

You're missing 'new' keyword. Also, this new one is presumably the width, since the outer one was the height.

coordinates.add(new ArrayList<My2DTableCoordinate>(height));

I would argue, however, that you generally should not declare variables to be a specific implementation of List. The only time you mention the specific implementation is when you create it. As long as you always declare things to be only as specific as you need to, you are more free to change later when you find out that a different implementation will have better performance. So I'd suggest the following.

class Foo {
    List<List<My2DTableCoordinate>> coordinates;

    public My2DTable(int height, int width) {
        coordinates = new ArrayList<>(height);
        for(int i = 0; i < height; i++) {
          coordinates.add(new ArrayList<My2DTableCoordinate>(width));
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM