Search code examples
javaandroidarraysobjectandroid-graphview

Extract an array in an nested array Java


I have the following code:

DataPoint dbPoint[] = new DataPoint[] {
                new DataPoint(0, 1),
                new DataPoint(1, 5),
                new DataPoint(2, 3),
                new DataPoint(3, 2),
                new DataPoint(4, 6)
        };

And I wish to remove new DataPoint(x, y) value inside, so I can do something like:

DataPoint dbPoint[] = new DataPoint[] {};
dbPoint[].add(new Data(1,1);
dbPoint[].add(new Data(2,5);
dbPoint[].add(new Data(3,7);
dbPoint[].add(new Data(4,3);
dbPoint[].add(new Data(5,9);

Thanks!


Solution

  • Build a List and convert to array using toArray().
    Remember, arrays cannot be resized, but collections like List can.

    ArrayList<DataPoint> points = new ArrayList<>();
    points.add(new DataPoint(0, 1));
    points.add(new DataPoint(1, 5));
    points.add(new DataPoint(2, 3));
    points.add(new DataPoint(3, 2));
    points.add(new DataPoint(4, 6));
    DataPoint[] dbPoint = points.toArray(new DataPoint[points.size()]);
    
    LineGraphSeries<DataPoint> series = new LineGraphSeries<>(dbPoint);