ArrayList中的数组问题,Java
我创建了一个多维数组,名为ArrayList中的数组问题,Java,java,arrays,multidimensional-array,jvm,Java,Arrays,Multidimensional Array,Jvm,我创建了一个多维数组,名为current\u map 我正在尝试访问当前地图: 当前地图[0][1] 但是,我收到了错误: 错误:需要数组,但找到字符串 这是我的代码,供您观赏 import java.util.*; import java.util.Map.Entry; import java.util.ArrayList; public class TestApp { private ArrayList<String[]> current_map = new ArrayL
current\u map
我正在尝试访问当前地图:
当前地图[0][1]
但是,我收到了错误:
错误:需要数组,但找到字符串
这是我的代码,供您观赏
import java.util.*;
import java.util.Map.Entry;
import java.util.ArrayList;
public class TestApp {
private ArrayList<String[]> current_map = new ArrayList<String[]>();
public TestApp() {
current_map.add(new String[] { "0","0","0" });
current_map.add(new String[] { "0","Q","0" });
current_map.add(new String[] { "0","0","0" });
}
public String getValue(int X,int Y){
String[] obj_map = current_map.toArray(new String[current_map.size()]);
return obj_map[X][Y]; // for example, getValue(2,2), should give "Q"
}
}
import java.util.*;
导入java.util.Map.Entry;
导入java.util.ArrayList;
公共类测试{
private ArrayList current_map=new ArrayList();
公开测试{
当前_map.add(新字符串[]{“0”、“0”、“0”});
当前_map.add(新字符串[]{“0”、“Q”、“0”});
当前_map.add(新字符串[]{“0”、“0”、“0”});
}
公共字符串getValue(整数X,整数Y){
String[]obj_map=current_map.toArray(新字符串[current_map.size()]);
return obj_map[X][Y];//例如,getValue(2,2)应该给出“Q”
}
}
如何停止此问题?您可以执行以下操作:
import java.util.*;
import java.util.Map.Entry;
import java.util.ArrayList;
public class TestApp {
private ArrayList<String[]> current_map = new ArrayList<String[]>();
public TestApp() {
current_map.add(new String[] { "0","0","0" });
current_map.add(new String[] { "0","Q","0" });
current_map.add(new String[] { "0","0","0" });
}
public String getValue(int X,int Y){
return current_map.get(Y)[X]; // for example, getValue(2,2), should give "Q"
}
public static void main(String[] args) {
TestApp ta = new TestApp();
System.out.println(ta.getValue(1, 1));
}
}
import java.util.*;
导入java.util.Map.Entry;
导入java.util.ArrayList;
公共类测试{
private ArrayList current_map=new ArrayList();
公开测试{
当前_map.add(新字符串[]{“0”、“0”、“0”});
当前_map.add(新字符串[]{“0”、“Q”、“0”});
当前_map.add(新字符串[]{“0”、“0”、“0”});
}
公共字符串getValue(整数X,整数Y){
返回当前_map.get(Y)[X];//例如,getValue(2,2)应该给出“Q”
}
公共静态void main(字符串[]args){
TestApp ta=新TestApp();
System.out.println(ta.getValue(1,1));
}
}
注意,在Java数组中,索引是基于0的,所以第二行第二列用(1,1)表示,而不是(2,2)
希望这能有所帮助。除非有充分的理由对每个get命令进行完整复制,否则您应该使用现有的结构
public String getValue(int X, int Y)
{
return current_map.get(X)[Y];
}
您所拥有的并不是真正的多维表示,因为您访问不同维度的方式并不一致。它的语义,但要称之为真正的多维(包括符号),您需要这样的东西(请参考此代码的源代码。我不是此代码的所有者。)
import java.util.ArrayList;
公共类ArrayList2d
{
数组列表数组;
公共ArrayList2d()
{
数组=新的ArrayList();
}
/**
*确保最小容量为num行。请注意,这并不保证
*有那么多行。
*
*@param num
*/
公共空间容量(整数)
{
array.ensureCapacity(num);
}
/**
*确保给定行至少具有给定的容量。请注意
*此方法还将确保getNumRows()>=行
*
*@param行
*@param num
*/
公共空间容量(int行,int num)
{
保证能力(世界其他地区);
while(行=getNumRows())
{
add(新的ArrayList());
}
获取(行)、添加(数据);
}
公共类型get(int行,int列)
{
返回数组.get(行).get(列);
}
公共无效集(整数行、整数列、类型数据)
{
数组.get(行).set(列,数据);
}
公共无效删除(整数行,整数列)
{
数组。获取(行)。删除(列);
}
公共布尔包含(类型数据)
{
对于(int i=0;i
您说过obj_map
是一个字符串[]
,但在下一行中,您将其视为二维数组
import java.util.ArrayList;
public class ArrayList2d<Type>
{
ArrayList<ArrayList<Type>> array;
public ArrayList2d()
{
array = new ArrayList<ArrayList<Type>>();
}
/**
* ensures a minimum capacity of num rows. Note that this does not guarantee
* that there are that many rows.
*
* @param num
*/
public void ensureCapacity(int num)
{
array.ensureCapacity(num);
}
/**
* Ensures that the given row has at least the given capacity. Note that
* this method will also ensure that getNumRows() >= row
*
* @param row
* @param num
*/
public void ensureCapacity(int row, int num)
{
ensureCapacity(row);
while (row < getNumRows())
{
array.add(new ArrayList<Type>());
}
array.get(row).ensureCapacity(num);
}
/**
* Adds an item at the end of the specified row. This will guarantee that at least row rows exist.
*/
public void add(Type data, int row)
{
ensureCapacity(row);
while(row >= getNumRows())
{
array.add(new ArrayList<Type>());
}
array.get(row).add(data);
}
public Type get(int row, int col)
{
return array.get(row).get(col);
}
public void set(int row, int col, Type data)
{
array.get(row).set(col,data);
}
public void remove(int row, int col)
{
array.get(row).remove(col);
}
public boolean contains(Type data)
{
for (int i = 0; i < array.size(); i++)
{
if (array.get(i).contains(data))
{
return true;
}
}
return false;
}
public int getNumRows()
{
return array.size();
}
public int getNumCols(int row)
{
return array.get(row).size();
}
}