java – 从内部类引用的局部变量必须是final或者有效的final
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 从内部类引用的局部变量必须是final或者有效的final,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2978字,纯文字阅读大概需要5分钟。
内容图文
![java – 从内部类引用的局部变量必须是final或者有效的final](/upload/InfoBanner/zyjiaocheng/809/e9f29397d6954f73a6c633f7cbb2e452.jpg)
这个程序是我班级的最后一个任务,我有问题弄清楚为什么我收到错误“从内部类引用的局部变量必须是最终的或有效的最终版本”.程序正在运行并发线程来对#的数组进行排序,然后找到该数组的高值和低值.当我在没有并发的情况下创建它时,我没有出现此错误.我在努力确定高低变量的最终位置.
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];最后,我得到一个错误,我不能使该值最终,相反变量的错误消失.
这是该计划的其余部分.任何帮助表示赞赏.
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()方法中更新高和低,根据定义使它们不是最终的.
因为在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全部内容,希望文章能够帮你解决java – 从内部类引用的局部变量必须是final或者有效的final所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。