简体   繁体   English

从内部类引用的局部变量必须是最终的或有效的最终

[英]local variables referenced from an inner class must be final or effectively 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);
}

This is the block of code producing the error. 这是产生错误的代码块。 If I make either the int high = numbers[0]; 如果我做int high = numbers [0]; or int low = numbers[0]; 或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);
}

} }

You keep updating both high and low inside the run() method, making them by definition not effectively final. 你继续在run()方法中更新highlow ,根据定义使它们不是最终的。

Since you don't need them outside the run() method anyway, just move the two lines inside. 因为在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: ");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JSlider和JTextField数组更改侦听器-从内部类引用的局部变量必须是最终的或实际上是最终的 - JSlider and JTextField array change listener - Local variables referenced from inner class must be final or effectively final 可运行的“从内部类引用的局部变量中的计数器必须是最终的或有效地是最终的” - counter in runnable 'local variables referenced from an inner class must be final or effectively final' 从内部类引用的Java MultiThreadding局部变量必须是有效final的final - Java MultiThreadding Local variables referenced from an inner class must be final of effectively final 错误:从内部类引用的局部变量必须是final或有效的final - error: local variables referenced from an inner class must be final or effectively final 从内部类引用的JAVA局部变量必须是final或有效的final - JAVA local variables referenced from an inner class must be final or effectively final “从内部类引用的局部变量必须是最终的或有效的最终变量”-如何解决? - "Local variables referenced from an inner class must be final or effectively final" - How to fix? 内部类引用的局部变量必须是最终变量或有效最终变量 - Local variables referred from inner class must be final or effectively final 内部类的局部变量必须是最终变量或有效最终变量 - Local variables from a inner class must be final or effectively final 从 lambda 表达式引用的局部变量必须是最终的或有效的最终变量 - local variables referenced from a lambda expression must be final or effectively final 如何修复从内部类引用的局部变量必须是最终的或有效的最终错误 - how to fix local variable referenced from an inner class must be final or effectively final error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM