Java,访问ArrayList的ArrayList元素
我确实看过,但我想在我的案例中有一个更简单的答案。还研究了似乎可以解释潜在的cast问题,但不能帮助我了解这里的最佳方法。谢谢你花时间阅读这篇文章 最终,我是一名Java新手,我只是想用读入数据结构的基于文本的二维映射实现一个映射类。因为我在阅读之前不知道地图的大小,所以我尝试了向量,但转到了ArrayList 编辑这里的“地图” 我在方法之外,就在类声明下面:Java,访问ArrayList的ArrayList元素,java,arrays,arraylist,casting,Java,Arrays,Arraylist,Casting,我确实看过,但我想在我的案例中有一个更简单的答案。还研究了似乎可以解释潜在的cast问题,但不能帮助我了解这里的最佳方法。谢谢你花时间阅读这篇文章 最终,我是一名Java新手,我只是想用读入数据结构的基于文本的二维映射实现一个映射类。因为我在阅读之前不知道地图的大小,所以我尝试了向量,但转到了ArrayList 编辑这里的“地图” 我在方法之外,就在类声明下面: // we have this vector here to be accessible to all methods // the
// 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方法中(整个方法在末尾):
在我尝试运行它之前,不会出现错误:
public static char getTerrainAtLocation( int row, int column )
{
char[] innerList = (char[])mapList.get(row);
return innerList[column];
}
我显然需要一些帮助!您的最佳建议是什么,我只想从“map”返回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
公共静态void readMapFile(字符串arg)
{
如果(arg!=“”)
{
尝试
{
//打开arg 0中给定文件的路径,这不会引发
//例外
Scanner fileIn=new Scanner(path.get(arg));
//需要清除新文件的mapVector(如果已清除)
//里面有数据
clear();
//这是将引发异常的行
//尝试读取该文件,看看它是否引发IOException
mapRows=fileIn.nextInt();
mapCols=fileIn.nextInt();
//读到最后一行
字符串mapFileLine=fileIn.nextLine();
//现在我们有了第一行read,包括行和列
//需要一个二维字符数组来保存地图
//char[][]mapArray=new char[mapRows][mapCols];
//当一行完成时,逐行生成行向量,
//将其添加到完整向量,生成一个向量向量
对于(int i=0;i
谢谢 Java的数据结构很棒,但你必须记住最基本的:)
FWIW我把地图文本放在上面你到处都在使用原始类型,你应该使用类型参数。八月,我几乎不明白,你指的是调用ArrayList模板时会发生什么?我知道一些C++,但对java有新的认识。1)泛型的原始类型每次都会把你引向这个兔子洞。具体键入
ArrayList
。2) 您正在尝试执行ArrayList
?mapList.get(row)
不会返回ArrayList
。它返回存储在该索引中的ArrayList
元素。谢谢,我太新了,无法对此进行更新投票。非常酷,它应该是Java中的高级编程类(后C#和C++),但我从未想过使用字节数组,这很酷!)
public static char getTerrainAtLocation( int row, int column )
{
char[] innerList = (char[])mapList.get(row);
return innerList[column];
}
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
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
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;
}
}