简体   繁体   English

将对象添加到数组值

[英]Adding an object to array values

I am not sure if title correctly relates to my problem, feel free to amend it! 我不确定标题是否与我的问题有关,请随时对其进行修改! :) :)

What i am trying to do is, visit amxn matrix in java and simultaneously mark the visited nodes based on certain condition 我想做的是,访问java中的amxn矩阵,并根据某些条件同时标记访问的节点

    //int a[][] = new int[5][5];
for(int i = 0;i< a.length;i++) //row
        for(int k = 0;k<a[0].length;k++) //column
            if((i+k) % 3 ==0 ) //condition
                a[i][k].visited = true;
            else
                a[i][k].visited = false;

In memory, i am imagining it something like : 在内存中,我在想像这样的东西:

 _ _ _ _
|       |       
|a[0][3]| - > Visited //(for true)  
|_ _ _ _|

But i am getting Error : 但我得到错误:

visited can not be resolved or is not a field 访问无法解决或不是字段

Can some one please help me on how to mark array blocks, or for instance, associate any value to them, like, just for example : 请问有人可以帮我标记数组块,或者例如,将任何值关联到它们,例如:

a[i][i].name = "Boston"
a[i][i].country = "India"

Dummy Program I am using , similar to my actual code 我正在使用的虚拟程序 ,类似于我的实际代码

class TestingGround {

    int a[][] = new int[5][5];
    boolean visited = false;

    public static void main(String[] args) {
        TestingGround tg = new TestingGround();
        tg.runner();

    }

    void runner()
    {
        for(int i = 0;i< a.length;i++)
            for(int k = 0;k<a[0].length;k++)
                if((i+k) % 3 ==0 )
                    a[i][k].visited = true;
                else
                    a[i][k].visited = false;
    }
}

You have made your two dimensional array using int , so you can only set each one to an integer value. 您已经使用int制作了二维数组,因此只能将每个数组设置为整数值。

In order to be able to set things like .name or .country you need to make an array of class objects that contain your requirements. 为了能够设置.name.country之类的内容,您需要制作一个包含您的要求的类对象数组。

Eg: describe your object (note: this is just a simple example, and should be improved) 例如:描述您的对象(注意:这只是一个简单的示例,应该加以改进)

public class MyObject
{
    public String name;
    public String country;
}

Then elsewhere: 然后在其他地方:

MyObject arr[][] = new MyObject[5][5];
arr[0][3].name = "SomeName";

When you access an array using the name[index] notation, what you're doing is accessing the object at that index in the array. 当您使用name[index]表示法访问数组时,您正在执行的操作是访问数组中该索引处的对象。 The methods you can call on this object depend on the static type of the array when you create it (casting can get around this, though). 创建对象时,可以在此对象上调用的方法取决于数组的静态类型(不过,广播可以解决此问题)。 For example, String[] array means array is an array of String objects, so if you do array[i] the methods you can perform are those you can perform on String objects. 例如, String[] array表示arrayString对象的数组,因此,如果执行array[i]则可以执行的方法是可以在String对象上执行的方法。

Thus, if you want to mark array blocks in the manner you describe in your question, the objects contained in the array have to be objects that have the fields you desire; 因此,如果要以问题描述的方式标记数组块,则数组中包含的对象必须是具有所需字段的对象; in your case, a has to be an array of objects that have name and country fields. 在您的情况下, a必须是具有namecountry字段的对象数组。 You made your array an int array, and as ints are primitives they do not have any fields; 您将数组设置为一个int数组,由于ints是基元,因此它们没有任何字段; thus, the compiler can't figure out what you mean by a[i][k].visited and so gives you an error. 因此,编译器无法弄清a[i][k].visited ,因此会给您带来错误。

If you have a single flag ( visited in your case), one possible option is to hold a second array that simply marks whether that position has been visited, but that probably isn't the most efficient way of doing so. 如果您只有一个标志(在您的情况下已visited ),则一个可能的选择是保留另一个数组,该数组仅标记该位置是否已被访问,但这可能不是最有效的方法。

For doing this array needs to be collection of Object not primitive types . 为此,该数组需要是Object而不是primitive types集合。

int a[][] = new int[5][5];

and

a[i][k].visited = false;

Obviously you can't do this. 显然,您不能这样做。

But what about this 但是这个呢

MyClass mc[][]=new MyClass[5][5];

Than you can access Visited which is boolean declared in class for that object. 比您可以访问Visited ,它是在该对象的类中声明的boolean

class MyClass
{
public boolean Visited;
    public MyClass(boolean b)
    {
       Visited=b;
    }
}

mc[0][0]=new MyClass(true);

//Then, access that variable

mc[0][0].Visited=false;

Note:You can not associate any variable to primiive value because it's not world famous Object . 注意:您不能将任何变量与原始值关联,因为它不是世界著名的Object

What you are trying to do is 你想做的是

a[0][0]----->Say 5
a[0][0].Visited=false-------> 5.Visited=false;(OOppss!!! Dosen't make any sense.)

First thing , a[i][k].visited can not be executed. 第一件事, a[i][k].visited无法执行。 because you are trying to access visited using 2d array object. 因为您正在尝试访问使用2d数组对象访问过的对象。 and 2d object is an int array object its not having visited with it 和2d对象是一个int数组对象,尚未visited

anything if you want to access it must be available within that object. 如果要访问它,则任何内容都必须在该对象中可用。

so define one class which should have visited boolean. 因此,定义一个应该访问布尔值的类。

create a class like this 创建这样的课程

class Matrix{

boolean visited;
}

then you can use like 那么你可以使用像

Matrix m[][] = new Matrix[10][10];

and set m[0][0].visited = true

You can't declare an int array and expect it to contain a variable named visited in it. 您不能声明一个int数组并期望它包含一个名为visited的变量。 You need to create an array of Objects. 您需要创建一个对象数组。

MyClass[][] myClassArray = new MyClass[5][5]; \\\\ Each index refers to MyClass instance \\\\每个索引都引用MyClass实例

and your MyClass definition should have the variable visited declared 并且您的MyClass定义应该声明已visited的变量

class MyClass{

public boolean visited;    

}

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

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