Java,访问ArrayList的ArrayList元素

时间:2014-10-14 22:48:12

标签: java arrays arraylist casting

我确实看过Accessing ArrayList<ArrayList<SomeObject>> elements,但我认为在我的案例中有一个更简单的答案。还看了Why does Java ArrayList use per-element casting instead of per-array casting?这似乎解释了潜在的演员问题,但并没有帮助我知道这里最好的方法。感谢您抽出宝贵的时间阅读本文!

最终,我是一名新手Java学生,只是尝试使用基于文本的二维地图读入一些数据结构来实现Map类。由于我在阅读之前不知道地图大小,我尝试了Vector,但转移到了ArrayLists。

编辑这里是“地图”

12 12
MwwwppppffffMMMM
MMwwppppfffffMMM
MMMwwwwwppppffff
ffffMMMMMwwwpppp
ffffMMMMMwwwpppM
MwwwppfMMMMppfff
MwwwpffffMMMMppp
MwwwppppffffMMMM
wwppppffffMMMMMw
wppppffffMMMMMww
ppppffffMMMMMwww
pppffffMMMMMwwwp

我在方法之外有这个,只是在类声明下:

// we have this vector here to be accessible to all methods
// the inner vector will be built in the readMapFile method
static private ArrayList mapList = new ArrayList();

然后在readMapFile方法中(整个方法结束):

        for ( int i = 0; i < mapRows ; ++i)
            {
                // read each subsequent line in the file
                mapFileLine = fileIn.nextLine();

                ArrayList mapRowList = new ArrayList();

                for ( int j = 0 ; j < mapCols ; ++j )
                {
                    // read each character on the line
                    mapRowList.add(mapFileLine.charAt(j));
                }

                // now put mapRowVector as an element of mapVector
                mapList.add(mapRowList); 

                System.out.println(mapList.get(i));

            }

然后我在尝试访问内部ArrayList中的元素时遇到了麻烦。有了这个,我得到“对象无法转换为ArrayList:

public static char getTerrainAtLocation( int row, int column )
{

    ArrayList innerList = mapList.get(row);
    return innerList[column];
}

在我尝试运行它之前,这不会出错:

public static char getTerrainAtLocation( int row, int column )
{

    char[] innerList = (char[])mapList.get(row);
    return innerList[column];
}

我显然需要一些帮助!这里你最好的建议是什么,我只想从“地图”中返回第x行和第y列的字符。我收到这个错误:

Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList cannot be cast to [C
at adventure2.Map.getTerrainAtLocation(Map.java:144)
at adventure2.Map.printMapList(Map.java:154)
at adventure2.Map.main(Map.java:56)
Java Result: 1

这是我完整的readMapFile:

public static void readMapFile( String arg )
{
    if ( arg != "")
    {
        try
        {
            // open a path to the file given in arg 0, this won't throw
            // an exception
            Scanner fileIn = new Scanner(Paths.get(arg));

            // need to clear mapVector for the new file, if it already
            // has data in it
            mapList.clear();

            // this is the line that will throw the exception
            // try to read the file, see if it throws IOException
            mapRows = fileIn.nextInt();
            mapCols = fileIn.nextInt();

            // read to the end of line
            String mapFileLine = fileIn.nextLine();

            // now we have the first line read, with rows and columns
            // need a 2-D char array to hold the map
            //char[][] mapArray = new char [mapRows][mapCols];

            // make up the row vectors row by row, when a row is complete,
            // add that to the full Vector, making a Vector of Vectors

            for ( int i = 0; i < mapRows ; ++i)
            {
                // read each subsequent line in the file
                mapFileLine = fileIn.nextLine();

                ArrayList mapRowList = new ArrayList();

                for ( int j = 0 ; j < mapCols ; ++j )
                {
                    // read each character on the line
                    mapRowList.add(mapFileLine.charAt(j));
                }

                // now put mapRowVector as an element of mapVector
                mapList.add(mapRowList); 

                System.out.println(mapList.get(i));

            }
        }
        catch ( IOException e )
        {
            System.out.println("There was an error reading the file sorry!");
        }
    }
    else
    {
        // arg length was 0 or negative
        System.out.println("Must call readMapFile with a filename as argument");
    } 
} // end readMapFile

谢谢!

1 个答案:

答案 0 :(得分:0)

Java的数据结构很棒,但你必须记住基础:)

class Terrain {

    private byte[] terrainData;
    private int terrainSizeX;
    private int terrainSizeY;

    public void init() {
        terrainData = new byte[terrainSizeX * terrainSizeY];
    }

    private int getIndex(int row, int column) {
        return terrainSizeX * column + row;
    }

    public byte getTerrainAtLocation(int row, int column) {
        return terrainData[getIndex(row, column)];
    }

    public void setTerrainAtLocation(int row, int column, byte value) {
        terrainData[getIndex(row, column)] = value;
    }
}