2

I'm trying to add a method an an array like this.

    Position[] positions = new Position[10];
    Position pos = positions.getPosAt(x, y);

I know this can be accomplished like:

   Position pos = getPosAt(positions, x, y)

But I would like to know if there is a way to accomplish the first method.

5
  • 3
    No, not in java. Commented May 14, 2019 at 14:42
  • 6
    As an alternative, you could create a class (wrapper) which would contain the positions and in which you would implement the method. But, again, this is just an alternative to the problem that in Java you cannot do that. Commented May 14, 2019 at 14:43
  • docs.oracle.com/javase/specs/jls/se7/html/jls-10.html Commented May 14, 2019 at 14:44
  • I'm really having trouble to understand your question because it looks like you're mixing arrays and classes in a way which is completely unintended and unusual. Commented May 14, 2019 at 14:45
  • Can be done in Kotlin with extension methods Commented May 14, 2019 at 14:46

2 Answers 2

3

you can make a class handler for this, like this PositionArray class (name it as you would like):

public class Test {
    
    public static void main(String... args) {

        Position[] positions = new Position[10];

        positions[0] = new Position(5, 10);
        positions[1] = new Position(11, 18);
        positions[2] = new Position(20, 7);

        PositionArray pa = new PositionArray(positions);

        System.out.println(pa.getPosAt(5, 10)); // Position{x=5, y=10}

    }
}
class PositionArray {

    private Position[] positions;

    public PositionArray(Position[] positions) {
        this.positions = positions;
    }

    public Position getPosAt(int x, int y) {

        for (Position p : positions) {
            if (!Objects.isNull(p)) {
                System.out.println(p.getX() + " " + p.getY());
                if (p.getX() == x && p.getY() == y) {
                    return p;
                }
            }
        }
        return null;
    }
}
class Position {

    private final int x;
    private final int y;

    public Position(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    @Override
    public String toString() {
        return "Position{" + "x=" + x + ", y=" + y + '}';
    }
    
}
Sign up to request clarification or add additional context in comments.

Comments

1

There is no way to do this in Java. You could possibly create your own class that contained an array of Position objects and provides e.g. get methods, but there is no way whatsoever to add methods to classes you do not control, including all array types.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.