I did look at Accessing ArrayList<ArrayList<SomeObject>> elements but was thinking there's a more simple answer in my case. Also looked at Why does Java ArrayList use per-element casting instead of per-array casting? which seems to explain the underlying cast issue but doesn't help me know the best approach here. Thanks for your time reading this!
Ultimately I am a newb Java student just trying to implement a Map class with a text-based 2-d map read into some data structure. As I don't know the map size before I read it, I tried Vectors but moved to ArrayLists.
EDIT here's the "map"
12 12
MwwwppppffffMMMM
MMwwppppfffffMMM
MMMwwwwwppppffff
ffffMMMMMwwwpppp
ffffMMMMMwwwpppM
MwwwppfMMMMppfff
MwwwpffffMMMMppp
MwwwppppffffMMMM
wwppppffffMMMMMw
wppppffffMMMMMww
ppppffffMMMMMwww
pppffffMMMMMwwwp
I have this outside of methods, just under the class declaration:
// 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();
Then in the readMapFile method I have (the whole method is at the end):
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));
}
Then I get in trouble trying to access the elements in the inner ArrayList. With this I get "Object cannot be converted into ArrayList:
public static char getTerrainAtLocation( int row, int column )
{
ArrayList innerList = mapList.get(row);
return innerList[column];
}
This doesn't give errors until I try to run it:
public static char getTerrainAtLocation( int row, int column )
{
char[] innerList = (char[])mapList.get(row);
return innerList[column];
}
I obviously need some help! What's your best advice here, I just want to return the char at row x and column y from a "map." I get this error:
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
Here's my complete 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
Thanks!