我有一个数据,可以直观地表示为12个折线图(频率),每个数据线中有2个系列(名为“L”,“R”),每个系列包含3000个X& Y值。
现在我将数据存储在2个对象中:
debug
我想将数据存储在1个对象中,但我不知道该怎么做。 一些伪代码:
function DoEvery(interval, obj, method)
local info = debug.getinfo(method, "S")
local id = info.linedefined..info.source
print(id)
-- "id" consists of line number and file name
-- where "method" function was defined
-- so it is unique
-- (unless you write two anonymous functions on the same line)
end
然后我想通过指定参数频率和线序列来检索X& Y值数组:
double[][][][] data = new double[12][2][3000][2];
double[] frequencies = new double[]{2.22, 2.3, 2.39,
2.48, 2.57, 2.67, 2.77, 2.89, 3.0, 3.18, 3.37, 3.57};
更新
感谢所有人的想法。我修改了我的要求。
List<Object[]> data = new ArrayList<Object[]>();
double[][] xyValues = new double[3000][2];
data.add{2.22, 'L', xyValues};
data.add{2.22, 'R', xyValues};
data.add{2.3, 'L', xyValues};
data.add{2.3, 'R', xyValues};
....
答案 0 :(得分:1)
我认为在不同的课程中将其拆分会让事情变得更简单。您可以使用Map将所有图表放入同一对象中,并按频率轻松获取给定图表。
class Chart {
final double[] leftData;
final double[] rightData;
Chart(double[] leftData, double[] rightData) {
this.leftData = leftData;
this.rightData = rightData;
}
}
以下是将数据填充到对象中的方法:
Map<Double, Chart> charts = new HashMap<>();
charts.put(2.22, new Chart(<leftData goes here>, <rightData goes here>));
charts.put(2.3, new Chart(<leftData goes here>, <rightData goes here>));
您可以稍后以这种方式阅读您的数据:
Chart chart = charts.get(2.22);
double[] leftData = chart.leftData;
double[] rightData = chart.rightData;
答案 1 :(得分:0)
为什么不使用对象?
免责声明:这是C#代码,而不是Java,但基本上适用相同的原则。
class DataPoint {
public double Frequency {get; set;}
public double Left {get; set;}
public double Right {get; set;}
public DataPoint( double freq, double l, double r ) {
Frequency = freq;
Left = l;
Right = r;
}
//Can insert sorting comparator functions here
// and inherit from IComparable for sorting in lists
}
List<DataPoint> dataset = new List<DataPoint>();
dataset.add(new DataPoint(2.22, 1234, 5678));
dataset.add(new DataPoint(2.3, 9876, 5432));
//Trying to get doubles based on equality can fail due to how doubles are stored,
// consider doing an absolute difference with some epsilon.
DataPoint point = dataset.Find(p => p.Frequency == 2.22);
double left = point.Left;
double right = point.Right;
当然,您可以根据需要详细说明结构(例如管理12个数据集),但这可能是一种前进的方式。
答案 2 :(得分:0)
如何对您的描述所描述的内容进行建模?
我有一个数据可以直观地表示为12个折线图(频率),每个数据中有2个系列(名为&#39; L&#39;,&#39; R&#39;)和每个系列包含3000个X&amp; Y值
所以你有12个折线图:
List<LineChart> lineCharts = new ArrayList<>(12);
折线图包含两个系列(L和R,我假设左右均值),显然是一个频率
public class LineChart {
private double frequency;
private LineSerie leftSerie;
private LineSerie rightSerie;
// ...
}
线系列包含3000个X&amp; Y值:
public class LineSerie {
private List<Point> values;
// ...
}
一个点包含X和Y
public class Point {
private double x;
private double y;
// ...
}
然后我想通过指定参数频率和线系列
来检索X&amp; Y值数组
所以你会这样做
LineSerie lineSerie =
lineCharts.stream()
.filter(chart -> chart.getFrequency() == 2.3)
.findAny()
.map(LineChart::getLeftSerie)
.orElseThrow(() -> new IllegalStateException("no chart with frequency 2.3");
答案 3 :(得分:0)
是的,您可以将它们存储在一个类中。让该类命名为LineChart
:
//for example only
public class Runner{
public static void main(String[] args){
ArrayList<LineChart> charts = new ArrayList<Linechar>();
charts.add(new LineChart()); //add your 12 line charts here..
}
}
public class LineSeries
{
private String name; //e.g "L", "R"
private List<Point> points; //to store your 3000 x,y points
}
public class LineChart
{
private List<LineSeries> lineSeriesList; //to store your "L"/"R" line series
}