• Post Reply Bookmark Topic Watch Topic
  • New Topic

Initialize Array of objects  RSS feed

 
Brent Van Scoy
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am working on the final touches of my Java 2 project. It's a basic GUI with two choice of computer models and you can add extra pieces.
My main questions is how do you initialize an array of objects? I have Googled, but it is not clear to me how to accomplish.

I've created an array:

private Component[] subTotalArray = new Component [11];

to store an object which contains my componentsName and componentsPrice.





I have a method I created to loop through the Array to gather my subtotal to set in a text field. The trouble I have run into is the loop crashes if there is not a object stored in the array [].
I want to initialize my array's first element to store the base descripition and cost, but it looks like I might need to initialize all of my elements?





Thanks for any help or push in the right direction.

Brent


 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, the array has a length independent of the number of Objects actually in the array. There are a couple of ways to do what you want:

1) In the for loop, check to see if the value stored in the array at the current index is null. If it is, skip it.
2) Put a dummy instance into the array for all positions when you initialize the array. The dummy instance would be one that has no side effects if it goes through processing, for example a Component with no name and a price of 0.0. You might be able to use one of the methods in the java.util.Arrays utility class to help with this... but am not sure, you might just have to loop and add it in manually yourself.
3) Use a List<Component> instead of Component[] since the size of the List is only as big as the number of Objects you put in.
 
Brent Van Scoy
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I was thinking of using dummy instance, but not sure how to populate the initial values. I could try to test for null value. I want to avoid an arrayList because of how I have it set up. I'm sure there are a million better ways, but I used the array and stored an object.

I've worked with other primitive types and initialized pretty easily, but initializing an object confuses me.

I've initialized several arrays in my project to populate the JComboBoxes

private String[] optical = {"Package Optical - $0.0","DVD Drive - Add $17.00",
"Combo DVD/CDRW - Add $40.00","DVD and CDRW - Add $79.00"

I get an incompatable type with this attempt.



Thanks, Brent
 
Brent Van Scoy
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried this also, but I must have it wrong, because it won't skip if null. Either that or it is not consider null, even if I have not placed any values.

I'll keep at it.

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brent Van Scoy wrote:I've worked with other primitive types and initialized pretty easily, but initializing an object confuses me.

Right, primitives have a default value, so if you don't assign them something, they will have a default. The Object reference in an array also has a default value - but that value is null because there is no real good 'default' to use.

If you wanted to do add a dummy object to each position. what I would do would be:
1) create a single dummy object
2) create the array of appropriate type and size
3) loop through the array
4) assign each index in the array the same dummy object

This would probably be best done in a method that returns the filled array.
 
Brent Van Scoy
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. I will try to work through your advice.

thanks!
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brent Van Scoy wrote:

The fact that you have to add that suppress warning is a hint that there is a problem, and what the problem is. You shouldn't suppress it, you should find it and fix it...
Brent Van Scoy wrote:

And this is the problem. That semi colon after the if statement ends the line of code that gets executed when the condition is true. So that line translates to:
"If the value in this position in the array is not null, do nothing special."
You should make it a habit to always make all your if statements into blocks, so you know where they begin and end:
 
Brent Van Scoy
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this works. Yes, I need to work on my blocks. I am pretty new at this, so I pretty sloppy. I am working on that also.

Thanks for the help. Brent

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!