3

I created a multidimensional array called current_map.

I am trying to access current_map:

current_map[0][1]

However I receive 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 issue?

0

4 Answers 4

6

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 in Java array indexes are 0-based, so 2nd row, 2nd column is represented with (1, 1), not (2, 2).

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

2

Unless there's compelling reason to do a full copy on every get command, you should use your existing structure.

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

Comments

2

You have said obj_map is a String[], but in the very next line you treat it as a 2D array.

Comments

1

What you have is not really a true multi-dimension representation as the way you access different dimensions is not consistent. Its semantics but to call it a true multi-dimensional (including symantics) you need something like this (Please refer 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();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.