gpt4 book ai didi

java - 从内部类引用的局部变量必须是 final 或有效的 final

转载 作者:搜寻专家 更新时间:2023-10-31 19:53:56 27 4
gpt4 key购买 nike

这个程序是我类(class)的最终作业,我无法弄清楚为什么我收到错误“从内部类引用的局部变量必须是最终的或有效的最终”。该程序正在运行并发线程来对# 数组进行排序,然后找到该数组的高值和低值。当我在没有并发的情况下创建它时,我没有出现这个错误。我正在为最终确定高变量和低变量的位置而苦苦挣扎。

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 high = numbers[0];或 int low = numbers[0]; 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);
}

最佳答案

您不断更新 run() 方法中的 highlow,根据定义使它们不是有效的 final。

因为无论如何在 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: ");

关于java - 从内部类引用的局部变量必须是 final 或有效的 final,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32707583/

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