0

I've spent a couple of days looking into this and, unfortunately, I can't find an answer to this that doesn't either 1. Not apply to my usage, or 2. Require a high amount of knowledge of this topic already So this is my question: In Java, certain record-like classes (like ArrayList) can be set to a type when you create a new instance of them:

ArrayList<Integer> list = new ArrayList<Integer>();

So my question is, how do you do that for your class? I've read quite a bit into the ArrayList library class, and I've noticed they use

<E>

I think I remember it meaning Element, but I could be making stuff up.

I have a class called Grid (for grid creation and all grid operations for my maps and stuff)

public class Grid {
    private int sizeX;
    private int sizeY;
    private double[][] values;

    public Grid(){
        this.sizeX = 10;
        this.sizeY = 10;
        this.values = new double[this.sizeX][this.sizeY];
        this.clear();
    }

    public Grid(int width, int height){
        this.values = new double[width][height];
        this.sizeX = width;
        this.sizeY = height;
    }

    public Grid(Grid sample){
        this.values = sample.toArray();
        this.sizeX = sample.sizeX;
        this.sizeY = sample.sizeY;
    }

    public int getSizeX(){
        return this.sizeX;
    }

    public int getSizeY(){
        return this.sizeY;
    }

    public Grid copy(){
        return new Grid(this);
    }

    public double[][] toArray(){
        return this.values.clone();
    }

    public void clear(){
        for(int rows=0; rows<this.sizeY; rows++){
            for(int cols=0; cols>this.sizeX; cols++){
                this.values[cols][rows] = 0;
            }
        }
    }

    public boolean isSquare(){
        return (this.values.length == this.values[0].length);
    }

    /*---------------------- Grid Operations Begin Here ----------------------*/


}

I skipped the grid operations because I don't feel that they're important in this case. Right now, to create a new grid I do

Grid map = new Grid();

And it creates a grid of doubles. But I don't want to do just that - I'd like to be able to create a grid of integers, floats, doubles, chars, or strings, and I'd like to to be of the form:

Grid<Double> map = new Grid<Double>();
or
Grid<String> map = new Grid<String>();

And then for that to change datatype of some of the return methods and variables (like double[][] values into string[][] values)

So how would I rewrite my code for the "values" variable to be of any of those? Would I just make it a "private Object[][]" instead? And then how would I check to see what datatype it is? And, of course, my operations (plus the "clear()" method) depend on the datatype, so how would I check what it is (to, for example, set clear() to set the values to 0 if int, but " " if String, or to have the method "toArray()" be of Integer, Float, String, Char, or Double?

I have no idea how these elements or types work at all right now, and I'd like to learn it fairly quickly, so please respond with full chunks of code and descriptions of it!

EDIT: I've had a slight, probably stupid idea. Would initialising things as Objects then casting them to E work? For example:

public class Grid<E>{
    private Object[][] values;

    public Grid(){
        this.sizeX = 10;
        this.sizeY = 10;
        this.values = new Object[this.sizeX][this.sizeY];
        this.clear();
    }

    public E toArray(){
        return (E[][]) this.values.clone();
    }

    public E get(int x, int y){
         return (E) values[x][y];
    }

    public void set(int x, int y, E value){
        this.values[x][y] = value;
    }
}

If the above would work/almost work, could you please tell me any problems I could have with it? If not, please ignore it and answer the original question :D

  • 1
    There is no shortcut around learning Generics. You got a link to the tutorial in the answer, but some notes: (1) Generics are not an answer if you have methods that are related to the specific type (for example, if the type is double, do a calculation. If the type is string, find a substring). They are for classes that are supposed to have the same operations for all allowed types. (2) Generics and arrays don't go well together. (3) There is basically no way to check what the base type of a generic class is. – RealSkeptic Oct 10 '15 at 16:58
  • I figured out the problem I had. Now that I knew it was called Generics, I managed to get everything working. What makes you say that Generics and Arrays don't go well together? – Marcel Troscianko Oct 11 '15 at 11:56
  • See http://stackoverflow.com/q/1817524/4125191 for example. – RealSkeptic Oct 11 '15 at 12:01

1 Answers1

0

The Java Tutorial on Generics explains the answer better than I could. Please see https://docs.oracle.com/javase/tutorial/java/generics/index.html

Essentially, you would start off:

public class Grid<E> {
    private int sizeX;
    private int sizeY;
    private E[][] values;

replacing 'double' with 'E' whereever it appears.

johnsgp
  • 93
  • 1
  • 8