1

I would like to use different variables to access the same data.

i.e. I have an array:

float[] vector = new float[3];

I would like each value in the array to also have its individual label i.e.:

vector[0] == pitch;
vector[1] == yaw;
vector[2] == roll;

I would like to use vector[] & pitch/yaw/roll interchangeably. When I pass all three values between two functions I want to refer to the array, however when I access them individually I would like to refer to them as pitch yaw and roll.

Is this possible in Java?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • @ Lee Meador Yes, I tried what Jace J McPherson suggested, but as pointed out, that does not work. @ Juvanis - I am of course aware of that option. However I was wondering if there where a 'less brute force' method of doing it, partly because it would be useful, partly 'academic interest' – user2620379 Jul 25 '13 at 21:36
  • Thanks for all the suggestions! – user2620379 Jul 26 '13 at 13:42

3 Answers3

5

This isn't possible the way you mean. You can't set pitch = 20 and get vector[0] == 20; primitives, at least, don't work that way in Java. What you could do, though -- what you should do -- is create a class named Vector with methods named setPitch, getPitch, etc, and use the float[3] as an internal implementation detail.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
2

You can't do this with a primitive float and an array of type float[]. Java does not support variables that are pointers or references to primitives.

However, there are a few workarounds.

First, you could make your own mutable reference type holding a float value.

MyFloat[] vector = new MyFloat[3] { new MyFloat(p), new MyFloat(y), new MyFloat(r) };
MyFloat pitch = vector[0];
MyFloat yaw = vector[1];
MyFloat roll = vector[2];

But it would probably be better to wrap your array in an object, and use methods to get the members by meaningful name, rather than variables.

public class Orientation {
  private float[] vector = new float[3];

  public float[] getArray() { return vector; }

  public pitch() { return vector[0]; }
  public yaw() { return vector[1]; }
  public roll() { return vector[2]; }

  public setPitch( float pitch ) { vector[0] = pitch; }
  public setYaw( float yaw ) { vector[1] = yaw; }
  public setRoll( float roll ) { vector[3] = roll; }
}

This gets you close -- although you can't just say pitch, you could say o.pitch().

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • Just noticed that Louis Wasserman had already suggested encapsulation. He should get the credit for that. I'm leaving this answer since it's filled out a bit more. – Andy Thomas Jul 25 '13 at 21:17
0

Passing a primitive data type in java only send a copy of its data to the requested method. If you are looking to pass a Reference to the value (rather than a copy), then you should use the Object form of the variables.

So, use Float in place of float. This will make your numbers Object variables rather than primitive data types.

Jace J McPherson
  • 450
  • 3
  • 13
  • 1
    -1: Even if you use `Float`, this won't be possible for two reasons: 1. `Float` object references (as other primitive wrapper classes) are **immutable**. 2. Java is **pass by value**, no matter what. Refer to [Is Java “pass-by-reference”?](http://stackoverflow.com/q/40480/1065197). – Luiggi Mendoza Jul 25 '13 at 20:41