当前位置: 首页 > 知识库问答 >
问题:

从内部类引用的局部变量必须是最终变量或实际上是最终变量

隆飞宇
2023-03-14

这个程序是我的类的最终赋值,我在弄清楚为什么我收到错误“从内部类引用的局部变量必须是最终的或实际上是最终的”时遇到了问题。该程序正在运行并发线程来对#的数组进行排序,然后找到该数组的高值和低值。当我在没有并发的情况下创建它时,我没有这个错误。我正在努力确定在哪里最终确定高变量和低变量。

public void HiLo(int[] numbers){

    int high = numbers[0];
    int low = numbers[0];

    Runnable r2 = new Runnable(){
        @Override
        public void run() {
            System.out.println("The highest value is: ");
            for (int index = 1; index < numbers.length; index++){
                if (numbers[index] > high)
                    high = numbers[index];
                System.out.println(high);
                }
            System.out.println();
            System.out.println("The lowest value is: ");
            for (int ind = 1; ind < numbers.length; ind++){
                if (numbers[ind] < low)
                    low = numbers[ind];
                System.out.println(low);
            }
        }
    };
    pool.execute(r2);
}

这是产生错误的代码块。如果我使int高=数字[0];或int-low=数字[0];final,然后我得到一个错误,我不能使该值为final,相反变量的错误消失。

这是程序的其余部分。感谢任何帮助。

package concurrentthread;

import java.util.Arrays;
import java.util.Scanner;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;


public class ConcurrentThread {

    static Executor pool = Executors.newFixedThreadPool(2);

public static void main(String[] args) {
    int size;

    Scanner keyboard = new Scanner(System.in);

    ConcurrentThread sort = new ConcurrentThread();
    ConcurrentThread hilo = new ConcurrentThread();

    System.out.println("This program will calculate the highest and lowest "
                + "numbers entered by the user \nand also sort them in "
                + "ascending order");
    System.out.println();
    System.out.print("How many numbers would you like in the array? ");
        size = keyboard.nextInt();

    final int[] numbers = new int[size];

    for (int index = 0; index < numbers.length; index++){
        System.out.print("Please enter a number between 1 and 100: ");
        numbers[index] = keyboard.nextInt(); 
    }

    System.out.println();
    sort.Sort(numbers);
    hilo.HiLo(numbers);

    //System.exit(0);
}

public void Sort(int[] numbers){
    int sort = numbers[0];

    Runnable r1 = () -> {
        Arrays.sort(numbers);
        System.out.println("The sorted values are: ");
        for (int index = 0; index < numbers.length; index++)
            System.out.print(numbers[index] + " ");

        System.out.println();
    };
    pool.execute(r1);
}

public void HiLo(int[] numbers){

    final int high = numbers[0];
    int low = numbers[0];

    Runnable r2 = new Runnable(){
        @Override
        public void run() {
            System.out.println("The highest value is: ");
            for (int index = 1; index < numbers.length; index++){
                if (numbers[index] > high)
                    high = numbers[index];
                System.out.println(high);
                }
            System.out.println();
            System.out.println("The lowest value is: ");
            for (int ind = 1; ind < numbers.length; ind++){
                if (numbers[ind] < low)
                    low = numbers[ind];
                System.out.println(low);
            }
        }
    };
    pool.execute(r2);
}

}

共有1个答案

庄新翰
2023-03-14

在<code>run()和<code>low,根据定义,它们实际上不是最终的。

由于您无论如何都不需要它们在run()方法之外,只需将两行移到里面。

public void HiLo(int[] numbers){

    Runnable r2 = new Runnable(){
        @Override
        public void run() {
            int high = numbers[0];
            int low = numbers[0];
            System.out.println("The highest value is: ");
 类似资料:
  • 我正在尝试从pojo创建一个函数,该函数在以下意义上使用细节类值的求和: 但是不知道为什么这一行< code > sum = sum . add(detail . getvalue());引发此错误: 从lambda表达式引用的局部变量必须是final或有效final 你能告诉我我做错了什么吗?谢了。

  • 问题内容: 我什至不知道此代码即使能正常工作也不会做任何事情,但是我不知道该怎么做才能摆脱“从内部类引用的局部变量必须是最终的或实际上是最终的”错误消息,该错误消息在以“ fireballRight [i]”开头的三行中显示。 任何指导将不胜感激,谷歌似乎没有帮助我这一点。 问题答案: 您尚未显示所有代码,但我怀疑添加了以下内容: 在循环内使用而不是将其用作数组的索引应该可以修复错误。 另外,如@

  • 问题内容: 这个问题已经在这里有了答案 : 为什么在匿名类中只能访问最终变量? (15个答案) 为什么实例变量“忽略Lambda表达式中使用的变量必须是最终变量或实际上是最终变量”警告[重复] (2个答案) Lambdas:局部变量不需要最终变量,实例变量不需要 (10个答案) 2年前关闭。 当我编写此代码时,我收到一个编译时错误,该错误是: “ lambda中的变量必须是final或有效的fin

  • 我试图在中的forloop中更新,因为导致一个新的ComppletableFuture实例,我需要将其重新分配给一个新变量以更新,但我不太确定如何

  • 今天我想做一些我想完成的项目,在那里我得到了一个异常,我不能从lambda表达式中引用局部变量。我有一个方法,其中我给出了两个值,该方法检查值对是否已经在HashMap中 当它结束时,我想读出布尔函数,需要知道他是否发现它成立= false我怎样才能在这个lambda中设置founded或者有没有其他方法可以做到这一点?

  • 我正在尝试返回对象。 但我得到了 从内部类引用的局部变量必须是最终变量或实际上是最终变量 on < code > stats = statistics如何返回对象,但要确保< code >。close();是否正在运行?