This site requires JavaScript, please enable it in your browser!
Greenfoot back
DanielG99
DanielG99 wrote ...

2018/8/26

Send numbers of one array between two classes

DanielG99 DanielG99

2018/8/26

#
i want to send the information of one array in one class to other class and play with that information with the second class and then take it back to the first one. i dont know how to send that information, please help.
danpost danpost

2018/8/26

#
You can use a "getter" and a "setter" method in the class where the array is located. Like the following (presuming the array holds int values):
// in Class_A
public int getArrayValue(int index)
{
    return arrayName[index];
}

public void setArrayValue(int index, int value)
{
    arrayName[index] = value;
}
Then in Class_B. you could use something like:
int arrayValue = ((Class_A)class_A_object_name).getArrayValue(index); // get value
// work with arrayValue
((Class_A)class_A_object_name).setArrayValue(index, arrayValue); // set value
You need to login to post a reply.