有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

ArrayList中的数组问题,Java

我创建了一个名为current_map的多维数组

我正在尝试访问current_map

current_map[0][1]

但是,我收到了错误:

error: array required, but String found

这是我的代码,供您观赏

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) 个答案

  1. # 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){
            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. # 2 楼答案

    除非有令人信服的理由对每个get命令进行完整复制,否则应该使用现有结构

    public String getValue(int X, int Y)
    {
      return current_map.get(X)[Y];
    }
    
  3. # 3 楼答案

    您所拥有的并不是真正的多维表示,因为您访问不同维度的方式并不一致。它的语义,但要称之为真正的多维(包括符号),您需要这样的东西(请参考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();
        }
    }
    
  4. # 4 楼答案

    您已经说过obj_map是一个String[],但在下一行中,您将其视为一个2D数组