简体   繁体   中英

Array reference type in a method

I have been given a starting code to work on a project, however I am confused about the following code and cant seem to find any examples online!

public static Entity[][] read(){ ... }

How can I handle this Entity to add new entries to an array, and then how can I return this?

The following constructor is invoked by a different class.

public World() {
    aWorld = new Entity[SIZE][SIZE];
    int r;
    int c;
    for (r = 0; r < SIZE; r++) {
        for (c = 0; c < SIZE; c++) {
            aWorld[r][c] = null;
        }
    }
    aWorld = FileInitialization.read();
}

I feel it would be much simpler if the array was just a parameter or if it were something like:

public static int[][] read(){ ... }

UPDATE:

The goal is to read from a file in the method read() and then assign the an entity to the correct location based on the location in the file. But I am not able to assign since the data types would be incompatible, Required is Entity, but I want to be able to set it to an int, char or String.

To add to an array of objects, you do exactly what you would with an array of primitives (eg int s), you just use Entity s. So if you want to add something to aWorld you use

aWorld[r][c] = new Entity(...); //with provided constructor's parameters
// or
aWorld[r][c] = existing_Entity; //for an Entity variable you already have

When you're done adding, you simply return the array aWorld .

If FileInitialization's static read() is going to return Entity[][], that's an entity array by itself. It means that you shouldn't iterate aWorld, rather assign the return value to it directly like

aWorld = FileInitialization.read();

Inside the read(), use that for loop you've made in the constructor and add a new Entity object as noted by Linus

Alright I would like to say thanks to all of you here as I was set on the right direction. But I would like to share my answer which should be simple and hopefully make someones life easier in the future.

To initialize the array of objects just do it as you would initialize any other array, in this case:

Entity[][] reference_name = new Entity[SIZE][SIZE];

To return this value, simply return the reference:

return reference_name;

Now the part where you actually modify an entry into your array. Lets say you have something like

public static void Entity[][] read() { .. }

you need to create a class file Entity.java (same name as the array type being passed)

In this case it would look something like this:

public class Entity {
private char appearance;

public Entity(char anAppearance) {

    appearance = anAppearance;
}

now to give this array an entry do something like this:

reference_name[0][0] = new Entity('X');

alright and in case you are wondering how to display this just add an accesor method to class Entity.

public char getAppearance() {

    return(appearance);
}

and to output:

System.out.println(reference_name[0][0].getAppearance(); );

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