0

I am not sure if title correctly relates to my problem, feel free to amend it! :)

What i am trying to do is, visit a m x n matrix in java and simultaneously mark the visited nodes based on certain condition

    //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;
    }
}
2
  • What is the type of a ? If it is int, then there is no property in int named visited. Can't we change the code to a[i][k] = 1;//1 = visited Or maybe change the array type to Boolean and use something like a[i][k]= true; Commented May 3, 2014 at 3:59
  • a contains numeric values which are distance, take it as a graph holding distance between 2 different places denoted by i and k...so i have to mark the city that i have visited already, to avoid infinite loops! Commented May 3, 2014 at 4:02

5 Answers 5

2

You have made your two dimensional array using int, so you can only set each one to an integer value.

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.

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";
Sign up to request clarification or add additional context in comments.

4 Comments

i added my complete dummy code, i have declared visited in my class...its just i am not able to relate!!
What do you mean by relate?
Primitives like int don't have fields you can set. All you can do with them is math and assignment. What's he's saying is that if you want to set a specific field what you need is an array of some kind of object that has the field(s) you want to set - not an array of primitives. Does that make sense?
In other words, a cannot be an int array if you want to set some property of it
1

First thing , a[i][k].visited can not be executed. because you are trying to access visited using 2d array object. and 2d object is an int array object its not having visited with it

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

Comments

1

For doing this array needs to be collection of Object not 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.

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.

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.)

Comments

1

When you access an array using the name[index] notation, what you're doing is accessing the object at that index in the array. 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.

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. You made your array an int array, and as ints are primitives they do not have any fields; thus, the compiler can't figure out what you mean by a[i][k].visited and so gives you an error.

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.

Comments

0

You can't declare an int array and expect it to contain a variable named visited in it. You need to create an array of Objects.

MyClass[][] myClassArray = new MyClass[5][5]; \\ Each index refers to MyClass instance

and your MyClass definition should have the variable visited declared

class MyClass{

public boolean visited;    

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.