0

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.

3
  • 6
    Use a class, Luke. Commented Feb 26, 2013 at 21:21
  • class? How...? have you some examples, please. Commented Feb 26, 2013 at 21:23
  • 2
    class Blah { float dir; boolean ch = true; ... } private Blah[] array; Commented Feb 26, 2013 at 21:24

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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?

1 Comment

You mean... private Object array1[][];?
0

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?

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.