• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing arrays to methods  RSS feed

 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what I'm doing wrong. Put the following code into dang.java, it compiles fine. When run it gives an Exception in parseFoo line 28. This has to be an error so basic my brain just doesn't see it.

Thanks!


 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the exception? My guess is NullPointerException. NullPointerExceptions always happen for the same reason - you try to use a reference that hasn't been assigned an Object. Why would the value in the array not have an object? Because you never gave it one. Creating an array doesn't fill the array.
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, NullPointerException. It happens on the line "bar[74].x = 74;". I added "System.out.printf("Length = %d\n", bar.length);" just before that line, it correctly prints 256.

My understanding is the line "Foo[] foo = new Foo[MAX_FOO];" creates an array of 256 Foo objects, with all x, y values initialized to 0. Is my understanding incorrect?

Guess I could fire up Eclipse, but I'm a CLI kind of guy.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many people make this mistake when beginning to work with arrays. When you do:

all you're doing is creating a new array object that can store Foo objects. However, you're not actually creating any Foo objects. You've made the container, but you need to fill it.
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel Christophel wrote:You've made the container, but you need to fill it.


The whole point to parseFoo() is to fill it
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Venolia wrote:My understanding is the line "Foo[] foo = new Foo[MAX_FOO];" creates an array of 256 Foo objects, with all x, y values initialized to 0. Is my understanding incorrect?

Yes your understanding is incorrect. That line creates an array capable of holding MAX_FOO Foo instances. You still have to create the instances and put them in place.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Venolia wrote:The whole point to parseFoo() is to fill it

Then it should make Foo objects and put them in the array.
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After the Foo[] foo = new Foo[256] line I put a for loop to get a new Foo() and stuff it into the array. Is there a better way to do it?

It works, I tried it. Just seems like an inelegant way to do things.

Thanks for all your help!
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is generally the way to do it. An alternate approach (that may save memory) is to wait until you need access to a particular element and instantiate it then.

Which one you use depends on requirements and use. You apparently need room for MAX_FOO instances, but do you need all of them to exist? Is it more common to have a sparse array (some Foos being used, but not most), or do you expect to actually use them all?

Which you choose depends on your needs.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Venolia wrote:After the Foo[] foo = new Foo[256] line I put a for loop to get a new Foo() and stuff it into the array. Is there a better way to do it?

It works, I tried it. Just seems like an inelegant way to do things.

Thanks for all your help!


That's how I'd do it
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help. Not only is my short demo program working, but my much longer actual program is both working and checked into version control. All that's left is to write parseFoo(). Now where did I put my Friedl book?

As for how sparse the array is, I'd love to fill it but, realistically, I'll only discover 1/2 to 3/4 of my foos.

 
Piet Souris
Master Rancher
Posts: 2042
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could consider using a HashSet<Foo>. Only problem is that you would need to
define an "equals" and a "hashcode". However, if your Foo as given here is pretty much
the Foo you are using, you might want to extend the class "Point".
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:You could consider using a HashSet<Foo>.

Hi Piet, I am not sure that a HashSet is a proper collection here, unless you know something about his requirements that I don't. The Set interface is not a good replacement for arrays because:

1) it isn't ordered, like an array is
2) it doesn't allow random access, like an array does
3) it does not allow duplicates, like an array does
4) it does not work well with objects whose values change, as it looks like the data discussed here will

A List<Foo> implementation might be a better fit, but only if you don't need a sparse array. But either way, without knowning more about what is actually needed, suggesting a collection of any sort is not easy to do.
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The foo object has 15 elements: 1 ArrayList, 1 String, the rest are ints. Most of the elements will change value often.
 
Piet Souris
Master Rancher
Posts: 2042
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Steve,

very true remarks, and I agree 100%.

My suggestion was only meant as an alternative to the use of a fixed size array, if only a part of it
is going to be used.

Greetings, Piet
 
Piet Souris
Master Rancher
Posts: 2042
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jim,

well, you have your program up and running, so the use of an array is perfect.

Greetz,
Piet
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see that the problem has been solved (as usual I'm a day late and at least a dollar short). For initializing the array, why not use the array.fill method? Is there something inherently wrong with it? According to the Oracle Documentation, array.fill(object[] a, object[] value) would take care of it.

I didn't notice it being mentioned in any of the other replies.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert D. Smith wrote:I see that the problem has been solved (as usual I'm a day late and at least a dollar short). For initializing the array, why not use the array.fill method? Is there something inherently wrong with it? According to the Oracle Documentation, array.fill(object[] a, object[] value) would take care of it.

I didn't notice it being mentioned in any of the other replies.

The only thing wrong with the array.fill(object[] a, object[] value) method is that it doesn't exist. If you meant Arrays.fill(Object[] a, Object value), the problem with that method is that it fills the whole array with the same instance which is not what the OP wanted - he wanted different instances of his Foo class in his array.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!