Array problems in ArrayList, Java

I created a multidimensional array named current_map

.

I am trying to access current_map

:

current_map[0][1]

However, I am getting the error:

error: array required but string found

Here is my code for your viewing pleasure.

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"
    }
}

      

How can I stop this problem?

+3


source to share


4 answers


You can do something like this:

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

}

      



Note that the Java array index is 0, so the second row, 2nd column is represented by (1, 1) and not (2, 2).

Hope it helps.

+6


source


Unless you have a reason to make a complete copy with every get command, you should use your existing structure.



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

      

+2


source


You said that obj_map

- it is String[]

, but in the very next line, you treat it as a 2D array.

+2


source


What you have is not really a true multidimensional view, since the way you access different dimensions is inconsistent. Its semantics, but in order to call it true multidimensional (including symantics) you need something like this (please refer to this for the source of this code. I am not the owner of this code.)

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

      

+1


source







All Articles