• Post Reply Bookmark Topic Watch Topic
  • New Topic

Auto-initializing an array of objects  RSS feed

 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing an automated test and want to store my test data in an array. I can't figure out how to initialize the array at compile time.


I'll have about 30 test cases, the data arrays are much larger than 2x2. I'd hate to code "tests[0].valid = true; tests[0].name = "Foo"; tests[0].data[0][0] = 1;" etc etc etc.

Is it possible to auto-initialize an array of objects like this?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you add a constructor to your class Tests:

then you can make initializing the array of tests look like this:

Are there always 2 x 2 numbers? Then you could do this to get rid of the new int[][]{{..., ...}, {..., ...}}:

 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That works, thanks!

There are over 100 elements in each data array, I don't think I'll be naming them a0, a1, etc
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!