如何检查代码中变量的更新/更改位置(java - eclipse)?
我在调试代码时遇到一些问题,因为我无法确定数组的更新位置。我已经查看了所有班级文件,我完全不知道发生了什么。
目前,我有一个设置,可以读取 csv 文件并将内容存储在 2D 数组中(public static double[][] myArray
)。然后,每当用户按下按钮时,我都会将此数组传递给方法。
奇怪的是,它第一次工作正常,但是在随后单击该按钮时,数据发生了变化,我无法弄清楚为什么。我搜索了代码,除了将其传递到方法中的位置之外,绝对没有对原始数组(即 myClass.myArray = newArray
)的引用。
发生这种情况有原因吗?或者,有人可以建议一种跟踪变量何时更新/更改的方法吗?
以下是代码的简要概述...
读取 csv 数据集并将其保存在变量 (ClassA
) 中:
public static double[][] myDataset;
// ...
private static void readdDataset(String filePath)
{
CsvReader reader = new CsvReader();
myDataset = reader.readDataset(filePath, true);
}
这是用于设置按钮操作的代码:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
new RunProgram().execute(); // create a swing worker to run the code in bg
}
});
这是一个压缩的代码向下版本 SwingWorker 类中的代码:
protected Void doInBackground()
{
Config cfgFile = someClass.createConfigFile();
someOtherClass.runMyProgram(cfgFile, ClassA.myDataset);
return null;
}
最后是运行该程序的代码:
public static void runRegression(Config cfgFile, double[][] dataset)
{
// Print out the first line in the array to see if it is the same every time (it should be!)
for(double value : dataset[0])
{
System.out.print( value + " ");
}
System.out.println();
// Do some other stuff....
}
I'm having some problems debugging my code as I can't work out where an array is getting updated. I have looked through all my class files and I'm completely stumped as to what is happening.
At the moment I have a setup whereby I read in a csv file and store the contents in a 2D array (public static double[][] myArray
). I then pass this array into a method whenever a user presses a button.
The weird thing is that it works fine the first time, however on all subsequent clicks of the button the data has changed and I can't work out why. I have searched through the code and there are absolutely no references to the original array (i.e. myClass.myArray = newArray
) apart from where I pass it into the method.
Is there a reason this is happening? Alternatively please could someone suggest a way to track when the variable gets updated/changed?
Here is a brief overview of the code...
Read in the csv dataset and save it in a variable (ClassA
):
public static double[][] myDataset;
// ...
private static void readdDataset(String filePath)
{
CsvReader reader = new CsvReader();
myDataset = reader.readDataset(filePath, true);
}
This is the code used to set up the buttons' action:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
new RunProgram().execute(); // create a swing worker to run the code in bg
}
});
Here is a condensed down version the code in the SwingWorker class:
protected Void doInBackground()
{
Config cfgFile = someClass.createConfigFile();
someOtherClass.runMyProgram(cfgFile, ClassA.myDataset);
return null;
}
Finally here is the code to run the program:
public static void runRegression(Config cfgFile, double[][] dataset)
{
// Print out the first line in the array to see if it is the same every time (it should be!)
for(double value : dataset[0])
{
System.out.print( value + " ");
}
System.out.println();
// Do some other stuff....
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您传递此数组的每个方法都有机会更改其内容。您的数组不是不可变的。
Every method you pass this array to has the opportunity to change its contents. Your array is not immutable.
从大纲或项目视图中,右键单击变量,然后选择“切换观察点”。
(您还可以设置条件断点,这很方便。)
不需要对原始数组的引用,对静态(可变)对象的任何引用都可以操作其内容。
From the outline or project view, right-click on the variable, and select "Toggle Watchpoint".
(You can also set conditional breakpoints, which is handy.)
There doesn't need to be a reference to the original array, any reference to a static (mutable) object can manipulate its contents.
所以我不能真正确定到底发生了什么,因为你似乎正在处理二维对象和类,并且我对它们有非常基本的理解。
然而,听起来您的问题实际上并不是二维方面,而是它使用的数据的存储。听起来您的按钮是问题所在,或者是在按下按钮之前出现问题,并且由于您没有向我们提供代码,所以我认为没有人可以帮助您。
尝试为该按钮设置一个临时旁路,使其仅自动触发一次,看看会发生什么。
至于调试,正常的方法是说
这将在控制台上打印一行
Arraychanged 1 4
(如果myArray[0] == 0
和myArray[1] == 4
)如果没有看到一些代码,我就无法再帮助你,而且我认为其他人也无法提供帮助。
So I can't really be sure what is going on for sure since you seem to be dealing with 2d object and classes and I have a very basic understanding on them.
However it sounds like your problem aren’t really the 2d aspects but the storage of the data it uses. It also sounds like your button is the problem or it's before the button gets pressed and since you haven't given us the code I don't think anyone can help you.
Try making a temp bypass for the button so it just auto fires once and see what happens.
As for the debugging the normal way is to say
This will print in a line on the console
Array changed 1 4
(ifmyArray[0] == 0
andmyArray[1] == 4
)I can't help you anymore without seeing some code and I don't think anyone else can help.