最终变量在没有任何=语句的情况下被改变。

huangapple go评论69阅读模式
英文:

Final variable being changed without any = statements

问题

            int[] mouseCoordinates = new int[2];   // 获取点击的坐标
            mouseCoordinates = mouseListenerExample.getCoordinates();

            final int[] baseCoordinates = mouseCoordinates; // 基准坐标(无点击)问题所在//

            int[][] totalCoordinates = new int[4][2];  // 收集4次鼠标点击的所有坐标


            for (int i = 0; i < 4; i++){ // 目标是获取4次点击的坐标

                while (mouseCoordinates[0] == baseCoordinates[0]){
                    mouseCoordinates = mouseListenerExample.getCoordinates(); // 问题出在这里:当 mouseListenerExample.getCoordinates() 发生变化时,mouseCoordinates 也会改变,baseCoordinates 也在改变,尽管不应该有这种情况发生。

                    if (mouseCoordinates[0] != baseCoordinates[0]) {
                        break;
                    }

                }

                totalCoordinates[i] = mouseListenerExample.getCoordinates();
                mouseListenerExample.setCoordinates(baseCoordinates);
                mouseCoordinates = baseCoordinates;
            }
英文:

I'm making an autoclicking project in java for personal use, and using the following method to get coordinates of a click from a class that extends MouseAdapter. The click is being done on a JFrame.

            int[] mouseCoordinates = new int[2];   //The coordinates of the click
            mouseCoordinates = mouseListenerExample.getCoordinates();

            final int[] baseCoordinates = mouseCoordinates; //The base coordinates (no click) which is this problem//

            int[][] totalCoordinates = new int[4][2];  //An array that collects all coordinates of 4 mouse clicks


            for (int i = 0; i &lt; 4; i++){ //the goal is to get the coordinates of 4 clicks

                while (mouseCoordinates[0] == baseCoordinates[0]){
                    mouseCoordinates = mouseListenerExample.getCoordinates(); //The problem occurs here: when mouseListenerExample.getCoordinates() changes, mouseCoordinates is changed, baseCoordinates is also changing, which it shouldnt, since there are no statements that say so.

                    if (mouseCoordinates[0] != baseCoordinates[0]) {
                        break;
                    }

                }

                totalCoordinates[i] = mouseListenerExample.getCoordinates();
                mouseListenerExample.setCoordinates(baseCoordinates);
                mouseCoordinates = baseCoordinates;
            }

Is there some statement that is changing baseCoordinates that I am missing?

答案1

得分: 1

你的 baseCoordinates 变量已被声明为 final,因此它是不可变的,但由于它是一个 int[] 或整型数组,它是一个引用变量,所以不能改变的是引用本身,而不是引用的状态,因此数组内部的整数是可以(在你的情况下确实)改变的。

你正在改变 mouseCoordinates 保存的值。由于 baseCoordinates 引用的是完全相同的 int[] 对象,这也会相应地改变 baseCoordinates 的值。如果你不希望它被改变,最好为最终变量创建一个全新的整型数组对象。

可以这样做:

final int[] baseCoordinates = new int[mouseCoordinates.length];
System.arraycopy(mouseCoordinates, 0, baseCoordinates, 0, mouseCoordinates.length);
英文:

Your baseCoordinates variable has been declared final, and so it cannot change, but since it is an int[] or int-array, it is a reference variable, and so what cannot change is the reference itself, not the state of the reference, and so the ints held within the array can (and in your case -- do) change.

You're changing the values held by mouseCoordinates. Since the baseCoordinates refers to the exact same int[] object, then this will likewise change the values for baseCoordinates. Best to create a completely new int object for the final variable if you don't want it changed.

Do something like:

final int[] baseCoordinates = new int[mouseCoordinates.length];
System.arraycopy( mouseCoordinates, 0, baseCoordinates , 0, mouseCoordinates.length );

答案2

得分: 0

所以在大致查看了一下后,我只是将数组替换为单独的整数,并在跳出循环之前添加了一个打印语句,这样就使得它正常工作了。

英文:

So after some looking around, i just replaced the arrays with individual ints, and added a print statement right before the break, and it made it work.

huangapple
  • 本文由 发表于 2020年9月22日 07:41:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64001236.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定