简体   繁体   中英

Java - create instance class of type?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM