定义Java中的动态事件处理程序 [英] Define dynamically event handler in Java

查看:99
本文介绍了定义Java中的动态事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我已经在Java中定义了两个数组。第一个数组有 M 细胞,第二个 N 细胞。假设每个单元可以有一个 0 1 值。

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.

在此程序中的第一阵列的每个单元将加入第二阵列的单元中的一个,但我们不知道哪一个将被连接到所述第二阵列的细胞(此连接是完全合乎逻辑的,例如我们刚知道数组1 [3] 是与数组2 [7] )。

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

所以现在我想要定义的事件处理这些关系,所以当其中一个单元格的波动从1到0,其配对的细胞波动。其实我想在运行时和动态的方式来定义事件处理程序,因为在它之前,我不知道在数组1 这其中的一个单元将配对与在小区数组2

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.

请问有什么解决此?

如果你觉得我能解决,而不动态事件处理这个问题请让我知道你的解决方案。

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

推荐答案

下面就是解决这个不使用的事件处理程序的方法。看看这适用于你在做什么。

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

第一,而不是两个数组,让我们用两个二维数组是 MX 1 NX 1

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 */ ];

第一个 / *您code * / 是您选择把条件为 1 0 数组1 的每个元素。第二个 / *您code * / 是你的决定数组1 的哪一个元素方法对应于每个元素的数组2

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.

现在在每个元素数组2 数组1 ,所以当你在一个更新值的元件从 0 1 的阵列,它也更新了其他阵列中的

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"

这篇关于定义Java中的动态事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆