[英]Java, accessing element of ArrayList of ArrayList
我確實看過訪問ArrayList <ArrayList <SomeObject >>元素,但我認為我的情況有一個更簡單的答案。 還研究了為什么Java ArrayList使用按元素進行轉換而不是按數組進行轉換? 這似乎可以解釋潛在的演員表問題,但並不能幫助我了解此處的最佳方法。 感謝您的閱讀!
最終,我是一名Java新手,只是試圖通過將基於文本的二維地圖讀入某種數據結構來實現Map類。 由於在閱讀地圖之前我不了解地圖的大小,因此嘗試使用Vectors,但將其移至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列的char。 我收到此錯誤:
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
謝謝!
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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.