简体   繁体   English

方法中的数组引用类型

[英]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. 目的是从read()方法中的文件中读取内容,然后根据文件中的位置将实体分配给正确的位置。 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. 但是我无法分配,因为数据类型将不兼容,Required是Entity,但是我希望能够将其设置为int,char或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. 要添加到对象数组,您可以使用原始数组(例如int )进行完全相同的操作,只需使用Entity So if you want to add something to aWorld you use 因此,如果您想向aWorld添加内容,请使用

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 . 完成添加后,只需返回数组aWorld

If FileInitialization's static read() is going to return Entity[][], that's an entity array by itself. 如果FileInitialization的静态read()将返回Entity [] [],则它本身就是一个实体数组。 It means that you shouldn't iterate aWorld, rather assign the return value to it directly like 这意味着您不应该迭代aWorld,而应像直接将返回值分配给它

aWorld = FileInitialization.read(); 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 在read()内部,使用在构造函数中创建的for循环,并添加一个新的Entity对象,如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) 您需要创建一个类文件Entity.java(与要传递的数组类型同名)

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. 好吧,以防万一您想知道如何显示它,只需将accesor方法添加到Entity类。

public char getAppearance() {

    return(appearance);
}

and to output: 并输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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