Reputation: 612
I have this:
private float dir = 0f;
private boolean ch = true;
private String = "No";
private int aB = 5;
How can I now, make an two dimensional array with this values?
e.g. array1[][]
:
{ {0f, "true", "No", 5} }
Or should I make an array like this?
private String array1[][];
Saving all values as a String
and then parse
each item?
But I think, that this is not a good programming.
Upvotes: 0
Views: 6280
Reputation: 7501
You can use an abstract array, IE using Object as the type.
Object array1[][];
However this looks more like you really want a class.
class MyObject {
private float dir;
private boolean ch;
private String str;
private int aB;
// getters and setters omitted
}
and then create an Array or Collection with this: MyObject[] array1
. This is usually better design if you can store Objects.
Upvotes: 2
Reputation: 624
Most of the time it's not a great idea to mix values like that. It's called a jagged array, and many languages let you do it. It's fine in theory but in practice it can make it pretty hard to maintain your code later on.
You generally want to have things really well defined with descriptive variable names. And since Java is a strongly typed language you probably want to make a class that has these values as member variables, and then create an array of that class. Then if you have any special manipulations to make you can make special methods that handle just those values.
Does that make sense?
Upvotes: 0
Reputation: 4067
Use objects rather than primitives, e.g.
Object[][] vars = {{ new Float(0f); Boolean.TRUE, "No", new Integer(5) }}
However, I'd have to question why you actually need to do this, possibly a design flaw?
Upvotes: 0