如何增加最终的整数变量?

发布于 2024-11-15 11:21:19 字数 772 浏览 2 评论 0原文

Eclipse 提供了 final 但我无法增加 i 变量。

@Override
public void onClick(View v) {
    final TextView tv = (TextView) findViewById(R.id.tvSayac);

    int i = 1;
    do {
        try {
            new Thread(new Runnable() {
                public void run() {
                    tv.post(new Runnable() {
                        public void run() {
                            tv.setText(Integer.toString(i));
                        }
                    });
                }
            });
            i++;
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } while (i < 16);
}

在此处输入图像描述

Eclipse is offering final but I can't increase the i variable.

@Override
public void onClick(View v) {
    final TextView tv = (TextView) findViewById(R.id.tvSayac);

    int i = 1;
    do {
        try {
            new Thread(new Runnable() {
                public void run() {
                    tv.post(new Runnable() {
                        public void run() {
                            tv.setText(Integer.toString(i));
                        }
                    });
                }
            });
            i++;
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } while (i < 16);
}

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

花期渐远 2024-11-22 11:21:19

Final 是初始化后不能更改的实体。

Final (Java)

你可以做的是在范围内创建一个变量do/while 循环最终具有 i 的值并将其发送到函数中。

A final is an entity that can not be changed after it is initialized.

Final (Java)

What you could do is create a variable within the scope of the do/while loop that is final with the value of i and send that into the function.

鼻尖触碰 2024-11-22 11:21:19

这里最简单的解决方案是创建一个类:

public class FinalCounter {

    private int val;

    public FinalCounter(int intialVal) {
        val=intialVal;
    }
    public void increment(){
        val++;
    }
    public void decrement(){
        val--;
    }
    public int getVal(){
        return val;
    }

    public static void main(String[] arg){
        final FinalCounter test = new FinalCounter(0);
        test.increment(); // 1
        test.increment(); // 2
        test.increment(); // 3
        test.increment(); // 4
        test.increment(); // 5
        test.decrement(); // 4
        System.out.println(test.getVal());  // prints 4
    }
}

The easiest solution here is to create a class:

public class FinalCounter {

    private int val;

    public FinalCounter(int intialVal) {
        val=intialVal;
    }
    public void increment(){
        val++;
    }
    public void decrement(){
        val--;
    }
    public int getVal(){
        return val;
    }

    public static void main(String[] arg){
        final FinalCounter test = new FinalCounter(0);
        test.increment(); // 1
        test.increment(); // 2
        test.increment(); // 3
        test.increment(); // 4
        test.increment(); // 5
        test.decrement(); // 4
        System.out.println(test.getVal());  // prints 4
    }
}
忱杏 2024-11-22 11:21:19

我认为可以创建变量i的本地副本。试试这个:

@Override
public void onClick(View v) {
    final TextView tv = (TextView) findViewById(R.id.tvSayac);

    int i = 1;
    do {
        final int localCopy = i; // Create here a final copy of i
        try {
            new Thread(new Runnable() {

                public void run() {
                    tv.post(new Runnable() {
                        public void run() {
                            // use here the copy
                            tv.setText(Integer.toString(localCopy));
                        }
                    });
                }
            }).start(); // Don't forget to start the Thread!
            i++;
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } while (i < 16);
}

通过创建最终的本地副本:

  • 编译器不会再
  • 因为 Java 按值复制而抱怨,您只会增加 i 而不是 localCopy

我想你也想启动线程...

编辑:确实,你是对的。您必须在循环内创建本地最终副本。检查新代码。

I think it is possible to create a local copy of the variable i. Try this:

@Override
public void onClick(View v) {
    final TextView tv = (TextView) findViewById(R.id.tvSayac);

    int i = 1;
    do {
        final int localCopy = i; // Create here a final copy of i
        try {
            new Thread(new Runnable() {

                public void run() {
                    tv.post(new Runnable() {
                        public void run() {
                            // use here the copy
                            tv.setText(Integer.toString(localCopy));
                        }
                    });
                }
            }).start(); // Don't forget to start the Thread!
            i++;
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } while (i < 16);
}

By creating a final local copy:

  • the compiler won't complain anymore
  • because of Java copies by value, you will only increase i and not localCopy.

I suppose you want to start the Thread as well...

EDIT: Indeed, you were right. You have to create the local final copy inside the loop. Check the new code.

树深时见影 2024-11-22 11:21:19

最终变量只能初始化一次,不一定是在定义它时初始化。它可以在构造函数中随时设置,但只能设置一次。在您使用 i++ 递增 i 的情况下,您试图再次将递增的值分配给 i ,这是不允许的。

A final variable can only be initialized once not necessarily when you are defining it. It can be set any time within the constructor , but only once. In your case when you are incrementing i using i++, you are trying to assign the incremented value to i again which is not allowed.

只为守护你 2024-11-22 11:21:19

您可以创建一个计数器类像这样并递增它。这样,Counter 对象的引用可能是最终的,但您仍然可以设置它的值?

You could create a counter class like that and increment it. This way, the reference of the Counter object could be final but you could still set its value ?

最单纯的乌龟 2024-11-22 11:21:19

我所做的是添加:

private int i;

在此之前:

@Override
public void onClick(View v) {
    final TextView tv = (TextView) findViewById(R.id.tvSayac);

    i = 1;
    do {
        try {
            new Thread(new Runnable() {
                public void run() {
                    tv.post(new Runnable() {
                        public void run() {
                            tv.setText(Integer.toString(i));
                        }
                    });
                }
            });
            i++;
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } while (i < 16);
}

之后您将能够像往常一样使用您的变量,而不必将其标记为最终变量。

What I did was add a:

private int i;

Before this:

@Override
public void onClick(View v) {
    final TextView tv = (TextView) findViewById(R.id.tvSayac);

    i = 1;
    do {
        try {
            new Thread(new Runnable() {
                public void run() {
                    tv.post(new Runnable() {
                        public void run() {
                            tv.setText(Integer.toString(i));
                        }
                    });
                }
            });
            i++;
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } while (i < 16);
}

And you'll be able to use your variable as usual after that, without having to mark it as final.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文