简体   繁体   中英

In which object store my data?

I have a data that can be visually represented as 12 line charts(frequencies) with 2 line series in each one(named 'L', 'R') and each line series contains 3000 X&Y values.

在此处输入图片说明

Now I'm storing my data in 2 objects:

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};

I want to store my data in 1 object, but I don't know how can I do this. Some pseudocode:

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};
....

Then I want to retrieve X&Y values array by specifying parameters frequency and line series:

double[][] xyValues = getXYValuesFromData(2.3, 'L');

Update:

Thanks all for your ideas. I made a revision of my requirements.

import java.util.ArrayList;
import java.util.Random;

public class Test {

public static void main(String[] args) {

    //class for storing my data
    ScanData myData = new ScanData();

    //-----------------------adding data-------
    //There are 4 possible series(L, R, I, V), I want put them independently,
    // so e.g. I can put just any one or any two
    myData.put(2.22, 'L', new XYvalues());
    myData.put(2.22, 'R', new XYvalues());
    myData.put(2.22, 'I', new XYvalues());
    myData.put(2.22, 'V', new XYvalues());

    myData.put(2.48, 'I', new XYvalues());
    myData.put(3.57, 'I', new XYvalues());

    //----------------------getting data--------
    //get xyValues of first frequency in my data and R-serie:
    double[][] xyValuesRserie = myData.freqIndex(0).r();
    //get frequency value of first frequency:
    double freq = myData.freqIndex(0).freq(); //2.22

    //get xyValues of l-serie by frequency value
    double[][] array = myData.freq(5.22).l();//[3000][2]
}

}

class XYvalues{

double[][] XYValues(){

    double[][] xyValues = new double[3000][2];
    Random random = new Random();

    for (int i=0; i< xyValues.length; i++){
        xyValues[i][0] = (double) (i - xyValues.length/2)*10;
        xyValues[i][1] = (double) random.nextDouble();
    }

    return xyValues;

}

}

class ScanData{

//?????

}

I think splitting this up in separate classes would make things simpler for you. You can use a Map to get all the charts into the same object and easily get a given chart by frequency.

class Chart {
   final double[] leftData;
   final double[] rightData;

   Chart(double[] leftData, double[] rightData) {
      this.leftData = leftData;
      this.rightData = rightData;
   }
}

Here is how you would fill data into your objects:

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>));

You can later read your data this way:

Chart chart = charts.get(2.22);
double[] leftData = chart.leftData;
double[] rightData = chart.rightData;

Why not use objects?

Disclaimer: This is C# code, not Java, but essentially the same principles apply.

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;

Of course, feel free to elaborate on the structure for your needs (such as managing 12 datasets), but this can be a way forward.

How about just modelling what your description says about what you have?

I have a data that can be visually represented as 12 line charts(frequencies) with 2 line series in each one(named 'L', 'R') and each line series contains 3000 X&Y values

So you have 12 line charts:

List<LineChart> lineCharts = new ArrayList<>(12);

A line chart contains two line series (L and R, which I assumed mean left and right), and, apparently, a frequency

public class LineChart {
    private double frequency;
    private LineSerie leftSerie;
    private LineSerie rightSerie;
    // ...
}

A line serie contains 3000 X&Y values:

public class LineSerie {
    private List<Point> values;
    // ...
}

A point contains an X and an Y

public class Point {
    private double x;
    private double y;

    // ...
}

Then I want to retrieve X&Y values array by specifying parameters frequency and line series

So you would do

LineSerie lineSerie = 
    lineCharts.stream()
              .filter(chart -> chart.getFrequency() == 2.3)
              .findAny()
              .map(LineChart::getLeftSerie)
              .orElseThrow(() -> new IllegalStateException("no chart with frequency 2.3");

Yes, you can store them in one class. Let that class be named 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM