变量无法解析 [英] Variable cannot be resolved

查看:20
本文介绍了变量无法解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个项目列表,每个 i 和 j 变量都不同.我的代码是:

I am trying to create an item list, diffrent for each i and j variable. My code is:

if (i == 0) { 
            if (j == 0) { 
                final CharSequence[] items = {"4:45", "5:00"}
            } else if (j == 1) { 
                final CharSequence[] items = {"4:43", "4:58"}
            } else if (j == 2) { 
                final CharSequence[] items = {"4:41", "4:56"}
            } else { 
                final CharSequence[] items = {"4:38", "4:53"}
}

...

new AlertDialog.Builder(this)
               .setTitle("Hours")
               .setItems(items,
                new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialoginterface, int i) {
                      // getStation(i);
                   }
                })
               .show();
       }

我在 .setItems(items,:

items cannot be resolved

我认为编译器认为 CharSequence[] items 可能没有初始化或什么的......我怎样才能让这个程序运行?

I think that the compiler thinks that the CharSequence[] items may not be initialised or something... How can I make this programme run?

推荐答案

问题是变量作用域.

if (someCondition) {
   final int i = 666;
} else {
   final int i = 42;
}
int j = i + 1; // compile-time error

这里我们有两个局部变量 i,它们在声明和初始化后立即超出范围.如果j需要i的值,那么i就必须在更大的范围内声明.

Here we have two local variables i who goes out of scope immediately after they're declared and initialized. If j needs the value of i, then i would have to be declared in a larger scope.

final int i;
if (someCondition) {
   i = 666;
} else {
   i = 42;
}
int j = i + 1; // compiles fine!

(应该提到,这正是三元运算符擅长的场景,即)

(It should be mentioned that this is exactly the kind of scenarios where the ternary operator excels, i.e.)

final int i = (someCondition) ? 666 : 42;

<小时>

在您的特定情况下,不幸的是,数组初始值设定项简写只能用于在声明时进行初始化.即:


In your specific case, unfortunately the array initializer shorthand can only be used to initialize upon declaration. That is:

int[] arr1 = { 1, 2, 3 }; // compiles fine!
int[] arr2;
arr2 = { 4, 5, 6 }; // doesn't compile!

您可以在 if 之外提取 items 的声明并为每个案例编写详细的代码(请参阅 Joachim Sauer 的回答),但更简洁的代码是改用数组数组.

You can pull out the declaration of items outside the if and write the verbose code for each case (see Joachim Sauer's answer), but a more concise code is to use array-of-arrays instead.

final CharSequence[][] allItems = {
   { "4:45", "5:00" },
   { "4:43", "4:58" },
   { "4:41", "4:56" },
   { "4:38", "4:53" }
};
final CharSequence[] items = allItems[j];

这种技术在这种情况下效果很好,但在更一般的情况下,您希望使用 Map 或类似的东西.

This technique works well in this case, but in the more general case you want to use a Map or something similar.

注意:原始代码中没有明确说明,但如果 j 可以是 012,则此方法有效3.如果您希望在 j 是除 012 之外的任何值时应用最后一个选项,则您必须检查并在此代码之前将其设置为 3.

Note: It's not explicit in the original code, but this works if j can either be 0, 1, 2, or 3. If you want the last option to apply when j is any value other than 0, 1, 2, then you have to check for that and set it to 3 before this code.

这篇关于变量无法解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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