0

Assume that I have defined two arrays in Java. The first array has m cells and the second has n cells. Assume that each cells can have a 0 or 1 value.

In this program every cell of the first array will join one of the cells of the second array, but we do not know which one will be connected to which cell of the second array (This connection is totally logical,e.g. we just know array1[3] is related to array2[7]).

So now I want to define an event handler for each of these relationship, so when one of the cells fluctuate from 1 to 0, its paired cell fluctuate. Actually I want to define event handler in a run-time and in dynamic way, because before it, I do not know which one of the cells in array1 will be pair with which one of cells in array2.

Is there any solution for this?

If you think I can solve this problem without dynamic event handler please let me know about your solution.

3
  • 1
    You cannot listen for array modifications using an event handler in Java. Commented Nov 7, 2013 at 15:27
  • you mean that there is not any way to track the changes of array cells? We can not define an event handler to notify us when value of a cell changed? Commented Nov 7, 2013 at 15:30
  • Nope. You would have to create your own object with an event handler and use two Lists to collect however many of the objects you want. Commented Nov 7, 2013 at 15:46

1 Answer 1

1

Here's a way to solve this without using an event handler. See if this works for what you are doing.

First, instead of two arrays, let's use two 2D arrays that are m x 1 and n x 1.

int[][] array1 = new int[m][];
int[][] array2 = new int[n][];
for (int i = 0; i < m; i++)
    array1[i] = new int[] { /* your code */ ? 1 : 0 };
for (int i = 0; i < n; i++)
    array2[i] = array1[ /* your code */ ];

The first /* your code */ is your condition for choosing to put either 1 or 0 into each element of array1. The second /* your code */ is your method for deciding which element of array1 corresponds to each element of array2.

Now each element in array2 is also an element of array1, so when you update a value in one of the arrays from 0 to 1, it is also updated in the other array.

array2[7] = array1[3];
array1[3][0] = 0;
System.out.println(array2[7][0]); // prints "0"
array1[3][0] = 1;
System.out.println(array2[7][0]); // prints "1"
Sign up to request clarification or add additional context in comments.

7 Comments

I think your solution is my answer, can you please describe your answer in details? I do not know what I should write instead of /*your code*/... can you help me to understand it??
@Chavoosh I've added the explanation into the answer. The reason I can't tell you what to write in place of /* your code */ is that you haven't told us how you decide which elements are 1 or 0, or how you choose the correspondence between elements of array1 and array2.
Thanks dear @Nathaniel for your response...I Have another question, in for section, you use array1 and array2 by one [] while you define 2d array, what this mean?
Dear @Nathaniel, I do not know how I can say thanks to you. Finally I got what you said and it works! Thanks a lot..... Just one another thing, each time that I change value of a cell in array1, its pair cell in array2 will change, now how I can find out which cell in array2 changed exactly that time?
@Chavoosh As you've probably figured out, a 2D array in Java is just an array of arrays. When you change the value of entry j in array1, the entry in array2 that changes will be entry i from your assignment array2[i] = array1[j];.
|

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.