【发布时间】:2013-02-10 18:04:37
【问题描述】:
我在另一个类中有一个函数,它产生值并将所有这些值存储在一个数组中。然后我返回了这个数组,因为我需要返回一个项目。我在函数末尾做了一个 println ,看起来像 System.out.println(outputArray[1]); 以查看是否正在存储值并且它确实返回了正确的值。该值为 3.68。
但是,当我尝试从另一个类访问数组时,我只得到 1 个 0 值,而不是存储在数组中的所有值。我在另一个类中再次执行了System.out.println(outputArray[1]);,它产生了 0 而不是 3.68。
为什么它不访问并返回存储在来自函数的索引处的数组中的值?
我认为这与我没有从该函数访问返回的数组有关。一旦我解决了这个问题,我将在这个其他类中使用这些值来绘制图表。
public class GetResults{
public double[] tableOfresults() {
double[] outputArray = new double[100];
for(int i = 0; i<100; i++) {
//At this point I calculate results and store them in the array using i as the index of the array
}
System.out.println(outputArray[1]); // produces 3.68 here
return outputarray;
public class graph{
getResults gr
public XYSeries inputOutputGraph() {
XYSeries graph = new XYSeries("My graph");
XYDataset xyDataset = new XYSeriesCollection(graph);
System.out.println(GetResults.outputArray[1];) //Its produces 0 here
JFreeChart chart = ChartFactory.createXYLineChart(
"Graph", "Time", "results",
xyDataset, PlotOrientation.VERTICAL, true, true, false);
ChartFrame graphFrame = new ChartFrame("XYLine Chart", chart);
graphFrame.setVisible(true);
graphFrame.setSize(300, 300);
return graph;
}
}
}
【问题讨论】:
-
可能是因为您没有访问正确的数组。如果没有看到相关代码,或者更好的是SSCCE,就不可能回答您的问题。
-
请发布示例代码!
-
@user2041029 我更新了我的答案。你能检查它是否有效吗?
标签: java jfreechart