gpt4 book ai didi

Java 操作监听器不适用于数组

转载 作者:行者123 更新时间:2023-12-01 22:58:18 25 4
gpt4 key购买 nike

这段代码有什么问题?当我单击按钮时,我试图让它转到下一个名为 bk 的排序数组。

Book[] bk = new Book[5];
bk = sortArray(bk);
final JTextArea textArea = new JTextArea();

JButton btnNext = new JButton("Next");
btnNext.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
for (int i = 0; i < bk.length; i++)
textArea.append("\n" + bk[i+1]);
}});

但是如果我将其声明为最终的,则排序的行会告诉我将最终声明取出。

这里是最终声明的错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The final local variable bk cannot be assigned. It must be blank and not using a compound assignment

并且没有最终声明:

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems: 
Cannot refer to a non-final variable bk inside an inner class defined in a different method
Cannot refer to a non-final variable bk inside an inner class defined in a different method

我该如何解决这个问题?

最佳答案

试试这个:

Book[] bk = new Book[5];
// fill bk here
final Book[] bkSorted = sortArray(bk);

关于Java 操作监听器不适用于数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23716863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com