从内部类引用的局部变量必须是final或有效的final [英] local variables referenced from an inner class must be final or effectively final

查看:95
本文介绍了从内部类引用的局部变量必须是final或有效的final的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该程序是我班上的最终作业,我在弄清楚为什么收到错误从内部类引用的局部变量必须是最终的或实际上是最终的"时遇到了问题.该程序正在运行并发线程以对#数组进行排序,然后查找该数组的高值和低值.当我没有并发创建它时,我没有这个错误.我在努力确定高和低变量的位置.

This program is the final assignment for my class and I'm have issues figuring out why I'm receiving the error "local variables referenced from an inner class must be final or effectively final". The program is running concurrent threads to sort an array of #'s and then find the high and low values of that array. When I created it without the concurrency, I didn't have this error. I'm struggling as to where to finalize the high and low variable.

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];最终,我得到一个错误,即我无法将该值定为最终值,相反变量的错误消失了.

This is the block of code producing the error. If I make either the int high = numbers[0]; or int low = numbers[0]; final then I get an error that I can't make that value final and the error for the opposite variable disappears.

这是程序的其余部分.感谢您的帮助.

Here is the rest of the program. Any help is appreciated.

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);
}

}

推荐答案

您不断在run()方法中更新highlow,从而使它们在定义上并没有最终效果.

You keep updating both high and low inside the run() method, making them by definition not effectively final.

由于无论如何您都不需要在run()方法之外使用它们,因此只需将两行移入其中即可.

Since you don't need them outside the run() method anyway, just move the two lines inside.

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: ");

这篇关于从内部类引用的局部变量必须是final或有效的final的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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