The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Array and Objects trouble

I'm trying to set the method "setArray()" with the values of "dimension" and "points" entered by a user. When I try to print it by calling the "getArray()" method, it prints "[[D@6818c458". Why is this happening?

I have made changes based on your answer. When ran, it will print the number of points the user asked for, however it only prints zeros. Is there an error happening when I set the array to the values of "points" and "dimension" to cause this to happen? (I have changed the code above to the updated code). Thanks

Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building

Posts

428

My Mood

Thanks

53

Thanked 67 Times in 67 Posts

Re: Array and Objects trouble

Originally Posted by incxx

I have made changes based on your answer. When ran, it will print the number of points the user asked for, however it only prints zeros. Is there an error happening when I set the array to the values of "points" and "dimension" to cause this to happen? (I have changed the code above to the updated code). Thanks

That is because java, I believe, will auto-initialize primitives (it doesn't do that with objects). Since you didn't call fillArray(), the data never got inserted into it, hence it was still in java's default initialization. I believe that java sets uninitialized number types to 0 by default.

Not exactly. It seems that setArray() is initializing your array. (It is not filling it, but making memory for the array itself.) Your default constructor doesn't do that so it will be null and will thrown an exception likely if you call fillArray() or anything with it before calling setArray().