java 调用全局数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/873696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:10:08  来源:igfitidea点击:

Calling a global Array

javaarraysglobal-variables

提问by Aaron Moodie

I'm currently trying to draw shapes with 2D Arrays. In my class there is a global array defined with public char canvas[][];

我目前正在尝试使用二维数组绘制形状。在我的班级中有一个定义为的全局数组public char canvas[][];

Up until now, I have only declared arrays with char canvas[][] = new char[height][width];

到目前为止,我只声明了数组 char canvas[][] = new char[height][width];

If this Array has already been declared, and I'm not supposed to amend the code I've been given, how do I call an instance of that array so that I can use it?

如果已经声明了这个数组,并且我不应该修改我得到的代码,我如何调用该数组的实例以便我可以使用它?

thanks.

谢谢。

(edit)

(编辑)

class DrawingSystem {

    public char canvas[][];

       public static void makeNewCanvas(int tmpWidth, int tmpHeight) {

        canvas[][] = new char[tmpHeight][tmpWidth];

        for (int row=0; row<tmpHeight; row++) {
            for (int col=0; col<tmpWidth; col++) {
                canvas[row][col] = ' ';
            }
        }       
    }

回答by bedwyr

You have an incompatibility between static methods and instance variables.

静态方法和实例变量之间不兼容。

Think about it this way: an instance variable is associated with a specific instanceof a class; a static variable is associated with the classitself. You call static methods via the class:

可以这样想:实例变量与类的特定实例相关联;静态变量与本身相关联。您通过类​​调用静态方法:

ClassI.callStaticMethod();

Whereas you call an instance method via an instance of the class:

而您通过类的实例调用实例方法:

public ClassI classObj = new ClassI();
classObj.callInstanceMethod();

In the code you posted, there's an instance variable ("canvas") being set in a static method (mainis associated with the Class, not an instance).

在您发布的代码中,在静态方法中设置了一个实例变量(“画布”)(main与类相关联,而不是与实例相关联)。

Therefore, you'll need to create instance methods to modify/update your "canvas", and create an instance of the class within the static function. This object (an "instance") can be used to update the instance variable.

因此,您需要创建实例方法来修改/更新您的“画布”,并在静态函数中创建类的实例。该对象(一个“实例”)可用于更新实例变量。

Here's an example:

下面是一个例子:

public class Foo {
    public char canvas[][];

    public static void main(String[] args) {
        Foo fooObj = new Foo(); //creates an instance of this class
        fooObj.createCanvas(2, 2);
        fooObj.modifyCanvas(0, 0, 'c');
    }

    public void createCanvas(int x, int y) {
        canvas = new char[x][y];
    }
    public void modifyCanvas(int x, int y, char c) {
        canvas[x][y] = c;
    }
}

This obviously isn't a one-to-one correlation to your assignment, but I'm sure you'll be able to adapt it to what you're doing :-)

这显然不是与您的任务一一对应的关系,但我相信您将能够使其适应您正在做的事情:-)

回答by nash

Your problem is that makeNewCanvas(int tmpWidth, int tmpHeight)is static or public char canvas[][]is not static.

您的问题是makeNewCanvas(int tmpWidth, int tmpHeight)静态的或非public char canvas[][]静态的。

In Java static class members can only work with other static class members. Static members belong to the class and non static members belong to instances. The class is a template that is used to create objects, these objects are called instances of the class. When you declare something static it is shared by all instances of the class. In the case of methods this means that static methods must behave exactly the same on all instances.

在 Java 中静态类成员只能与其他静态类成员一起使用。静态成员属于类,非静态成员属于实例。类是用于创建对象的模板,这些对象称为类的实例。当你声明一些静态的东西时,它被类的所有实例共享。在方法的情况下,这意味着静态方法在所有实例上的行为必须完全相同。

DrawingSystem a = new DrawingSystem();
DrawingSystem b = new DrawingSystem();

aand bare instance of the class DrawingSystemthat means they each have their own canvasarray. Now since makeNewCanvasis static and must behave the same for all instances it cannot use a.canvasor b.canvasbecause they are unique to aand band can have different contents.

a并且b是类的实例,DrawingSystem这意味着它们每个都有自己的canvas数组。现在既然makeNewCanvas是静态的,并且必须具有相同的行为它不能使用所有实例a.canvasb.canvas因为他们是唯一的ab,可以有不同的内容。

回答by abhilash

I'm not sure if I got your question right,

我不确定我是否回答对了你的问题,

But looks like you need a Singleton pattern, instead of declaring the char canvas[][]as a public field, encapsulate the canvas array as read only property

但是看起来您需要一个单例模式,而不是将其声明char canvas[][]为公共字段,而是将画布数组封装为只读属性

public class MyDrawing 
{

private char canvas[][] = null;

public char[][] getCanvas()
{
   if (canvas!=null)
   {
      canvas =new char[height][width];
   }
   return canvas;
}

When use getCanvas()in other parts of your code when you need the canvas array instead of the previously used canvas public variable.

getCanvas()您需要画布数组而不是以前使用的画布公共变量时,在代码的其他部分使用时。

回答by bryansh

Kinda confused about what you are asking for.

有点困惑你在问什么。

You can always redeclare the global array at any point, and use it for your own needs...but that seems rather suspect (meaning...why would this be a global array).

您始终可以随时重新声明全局数组,并将其用于您自己的需要……但这似乎很可疑(意思是……为什么这是一个全局数组)。

It seems like your canvas shouldn't be changing sizes...but than again, I don't know very much about your circumstances.

看起来你的画布不应该改变大小......但再说一次,我不太了解你的情况。

Check to see if the array is null, if so declare it to be the size that you want and use it happily?

检查数组是否为空,如果是,则将其声明为您想要的大小并愉快地使用它?