在ArrayList中使用数组出现问题,Java

3
我创建了一个名为current_map的多维数组。
我试图访问current_map: current_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"
    }
}

我该如何解决这个问题?
4个回答

6
您可以这样做:
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));
    }

}

请注意,在Java中,数组的索引是从0开始的,因此第二行第二列用(1,1)表示,而不是(2,2)。

希望这可以帮到您。


2

除非有充分的理由在每个获取命令中执行完整复制,否则您应该使用现有结构。

public String getValue(int X, int Y)
{
  return current_map.get(X)[Y];
}

2

您说过obj_map是一个String[],但在接下来的一行中,您却将其视为2D数组。


1

您所拥有的实际上并不是真正的多维表示,因为您访问不同维度的方式并不一致。这是语义问题,如果要称之为真正的多维(包括语义),则需要像这样的代码(请参考this以获取代码来源。我不是该代码的所有者。)

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();
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接