rshaq
rshaq

Reputation: 149

Instantiate Array of Arrays in one line

I am making a Java class where hierarchical data is stored. It is very similar to XML, and I would keep the entire structure in XML to be parsed later by Java, but we want to avoid the I/O overhead.

Instead, I want to keep the data hard-coded in Java objects in global variables. The XML code would look like this:

<Group name="Order">
   <Milestone name="Tested" value="testedDate"/>
   <Milestone name="Created">
      <Milestone name="Delivered" value="deliveredDate"/>
      <Milestone name="Pressed" value="someString"/>
      <Milestone name="Assembled" value="whatever"/>
   </Milestone>
   <Milestone name="Ordered" value="something"/>
</Group>

So I created classes with setters and getters and polymorphic constructors. The Group class has one constructor, takes the name attribute as a String input and an array of Milestone objects as another input:

public Group(String name, Milestone[] milestones) { ...... }

Note that the Milestone element however can have subelements of the same type. So I created a Milestone class with two constructors. The first constructor handles the first Milestone element. It takes just the name and value attributes as String inputs. The second constructor takes the name attribute as a String input, and also an array of Milestone objects as a second input.

public Milestone(String name, String value) { .....}
public Milestone(String name, Milestone[] subMilestones) { ...... }

But now what I want to do is instantiate all of this in one line of code, like this:

Group[] groups = {
   new Group("Order",{
      new Milestone("Tested","testedDate"),
      new Milestone("Created",{
         new Milestone("Delivered","deliveredDate"),
         new Milestone("Pressed","someString"),
         new Milestone("Assembled","whatever")})
      new Milestone("Ordered","something")
   }
   new Group("Invoice",...
   ...
   ...
};

It doesn't appear to like that. In short, it doesn't like me opening up brackets to start a new array of "new" Milestone objects within the declaration of Group array. Is there a way to do this all within one line of code (as in, in one declaration with one semicolon at the end)?

Upvotes: 0

Views: 653

Answers (2)

Seelenvirtuose
Seelenvirtuose

Reputation: 20618

From the variable declaration

Group[] groups = ...

the compiler only knows that groups is an array of Group objects. It does not know the internal structure. Nevertheless, this internal structure can be built in single statement, but you have to declare it:

Group[] groups = {
   new Group("Order", new Milestone[] {
      new Milestone("Tested","testedDate"),
      new Milestone("Created", new Milestone[] {
         new Milestone("Delivered,"deliveredDate"),
         new Milestone("Pressed,"someString"),
         new Milestone("Assembled,"whatever"))}
      new Milestone("Ordered","something")
   }
   new Group("Invoice",...
   ...
   ...
};

Upvotes: 0

Jim Garrison
Jim Garrison

Reputation: 86774

You have to explicitly instantiate the sub-arrays, as in

Group[] groups = {
   new Group("Order", new Milestone[] {
      new Milestone("Tested","testedDate"),
      new Milestone("Created", new Milestone[] {
         new Milestone("Delivered,"deliveredDate"),
         new Milestone("Pressed,"someString"),
         new Milestone("Assembled,"whatever")}),
      new Milestone("Ordered","something")
   }
   new Group("Invoice",...
   ...
   ...
};

Upvotes: 3

Related Questions