-1

I'm making a program to get the midpoint of a line. However i'm running into a problem.

What i need to be able to do is have as many points as i want, and dimensions. However i have no idea how to do this

So let me give you an idea of what i want to do.

points[point][x]=1;  points[point][y]=2;  points[point][z]=3;

See what i'm getting at? This is what i currently have

public float[][] points={{}};

And when i want to write to it

        for(int i=0; i<parts.length;i++){
            points[currentPoint][i]=Float.valueOf(parts[i]);
        }

java.lang.ArrayIndexOutOfBoundsException: 0

So how can i do this?

  • 2
    possible duplicate of [Initialising a multidimensional array in Java](http://stackoverflow.com/questions/1067073/initialising-a-multidimensional-array-in-java) – Foggzie Sep 20 '14 at 04:16
  • Leave off the right side on your declaration – ElefantPhace Sep 20 '14 at 04:23
  • GuntherFox. When i do that i get the error listen at the bottom of my post. ElefantPhace, when i do that i get a nullPointerException error – Matthew NotReal Sep 20 '14 at 04:36

2 Answers2

0

Use this to create a 3x3 2-D array and initialize all values to 0:

public float points[][] = new float[3][3];

And this to iterate through the array and set values:

for (int j=0; j<3; j++) {
    for (int i=0; i<3; i++) {
        points[j][i] = 42; //Or whatever
    }
}
AkraticCritic
  • 2,595
  • 2
  • 12
  • 11
0
public float[][] points={{}};

This creates an empty two dimensional array. Hence, the ArrayIndexOutOfBoundsException. If you want to create an array to hold 6 points and 3 dimensions then you have to allocate it:

public float[][] points = new float[6][3];

However, I would reconsider your design. If you would like to go more object oriented way than you would probably like to have a class for line as well as for point. For example:

class Line {
    private final Point startPoint;
    private final Point endPoint;

    Line(Point startPoint, Point endPoint) {
        this.startPoint = startPoint;
        this.endPoint = endPoint;

        final int startPointDimensions = startPoint.getDimensions().size();
        final int endPointDimensions = endPoint.getDimensions().size();
        if (startPointDimensions != endPointDimensions) {
            throw new RuntimeException("Points that form a line must have the same dimension! " +
                    "Start point dimension: " + startPointDimensions +
                    "End point dimension: " + endPointDimensions);
        }
    }

    public Point midpoint() {
        // your code goes here
    }
}

class Point {
    private final List<Float> dimensions;

    Point(List<Float> dimension) {
        dimensions = dimension;
    }

    public List<Float> getDimensions() {
        return dimensions;
    }
}

You can read more about lists in the oracle's documentation. I would prefer lists to arrays if you do not know up front how many dimensions you want to support or if you want to support different number of dimensions.

karolovbrat
  • 116
  • 4