英文:
Create Runnable with "final" variable from the parent?
问题
Create multiple new Runnable inside a loop ?
在循环内部创建多个新的Runnable?
I want to create multiple new instances of Runnable that calls another method and passing the "i" parameter from the parent's loop.
我想创建多个新的Runnable实例,这些实例调用另一个方法,并传递父循环中的"i"参数。
I do not want to use Threads since those runnables will be used as actions for keyboard shortcuts (using KeyCombination and accelerators).
(ex: Ctrl + 1, Ctrl + 2, ... Ctrl + 9)
我不想使用线程,因为这些Runnable将用作键盘快捷方式的操作(使用KeyCombination和加速器)。
(例如:Ctrl + 1、Ctrl + 2、... Ctrl + 9)
The content of the runnable that I want looks like this :
我想要的Runnable的内容如下:
if (shortcutsEnabled && checkDestinationValidity(i)) {
copyImage(i);
}
possibly important : the number of "i" will go up to 9.
可能重要的是:"i"的数量将增加到9。
Is there any way to do this without having to make a premade table of Runnable ?
有没有办法做到这一点,而不必创建预先制作的Runnable表格?
When I try to just put the variable inside, the NetBeans IDE shows me this message :
local variables referenced from a lambda expression must be final or effectively final
当我尝试将变量放在内部时,NetBeans IDE显示了以下消息:
"从lambda表达式引用的局部变量必须是final或有效final"
I have found a solution that has delayed my "deadline" for the solution :
我找到了一个解决方案,这个解决方案推迟了我的解决方案的"截止日期":
for (int i = 0; i < copyKeyCombinations.size(); i++) {
final int index = i;
Runnable copyRunnable = () -> {
if (shortcutsEnabled && checkDestinationValidity(index)) {
copyImage(index);
}
};
accelerators.put(copyKeyCombinations.get(i), copyRunnable);
}
For the record, the runnable are registered before the loop ends, and the solution above works but I was asked to find another solution
值得注意的是,在循环结束之前,这些Runnable已经被注册,上面的解决方案有效,但我被要求找到另一种解决方案
However, this is a solution that is not accepted for my work situation, I am looking for any alternatives.
然而,这个解决方案在我的工作情况下不被接受,我正在寻找任何其他替代方案。
(Note : this loop will only be launched one time, at the start of the app).
(注意:这个循环只会在应用程序启动时启动一次)。
英文:
Create multiple new Runnable inside a loop ?
I want to create multiple new instances of Runnable that calls another method and passing the "i" parameter from the parent's loop.
I do not want to use Threads since those runnables will be used as actions for keyboard shortcuts (using KeyCombination and accelerators).
(ex: Ctrl + 1, Ctrl + 2, ... Ctrl + 9)
The content of the runnable that I want looks like this :
if (shortcutsEnabled && checkDestinationValidity(i)) {
copyImage(i);
}
possibly important : the number of "i" will go up to 9.
Is there any way to do this without having to make a premade table of Runnable ?
When I try to just put the variable inside, the NetBeans IDE shows me this message :
local variables referenced from a lambda expression must be final or effectively final
I have found a solution that has delayed my "deadline" for the solution :
for (int i = 0; i < copyKeyCombinations.size(); i++) {
final int index = i;
Runnable copyRunnable = ()-> {
if (shortcutsEnabled && checkDestinationValidity(index)) {
copyImage(index);
}
};
accelerators.put(copyKeyCombinations.get(i), copyRunnable);
}
For the record, the runnable are registered before the loop ends, and the solution above works but I was asked to find another solution
However, this is a solution that is not accepted for my work situation, I am looking for any alternatives.
(Note : this loop will only be launched one time, at the start of the app).
答案1
得分: 0
您可以按照“标准”方式实现 `Runnable`,作为一个类,而不是作为 lambda 函数:
```java
public class CopyRunnable implements Runnable {
private final int index;
private final boolean shortcutsEnabled;
public CopyRunnable(int index, boolean shortcutsEnabled) {
this.index = index;
this.shortcutsEnabled = shortcutsEnabled;
}
@Override
public void run() {
if (shortcutsEnabled && checkDestinationValidity(index)) {
copyImage(index);
}
}
}
for (int i = 0; i < copyKeyCombinations.size(); i++) {
Runnable copyRunnable = new CopyRunnable(i, shortcutsEnabled);
//对其进行一些操作
}
<details>
<summary>英文:</summary>
You can implement `Runnable` the "standard" way, as a class, not as lambda function:
public class CopyRunnable implements Runnable {
private final int index;
private final boolean shortcutsEnabled;
public CopyRunnable(int index, boolean shortcutsEnabled) {
this.index = index;
this.shortcutsEnabled = shortcutsEnabled;
}
@Override
public void run() {
if (shortcutsEnabled && checkDestinationValidity(index)) {
copyImage(index);
}
}
}
for (int i = 0; i < copyKeyCombinations.size(); i++) {
Runnable copyRunnable = new CopyRunnable(i, shortcutsEnabled);
//do something with it
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论