Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to create new class instance on the fly - do I need an inner class?

 
Nigel Shrin
Ranch Hand
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I would like to call this method for a range of numbers, and store the results in a class called Set4 (as the results are a set of 1 to 4 numbers).
variable count tells me how many instances will be required, but how do I create and use them within the loop, do I need an inner class?
What methods could one use to get around this efficiently? (I am not experienced with inner classes at this point.)

ERROR: (only one number sent from calling program at this point; value 100)

1: 5 5 5 5
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at numbers.Lagrange4sq.process4sq(Lagrange4sq.java:35)

line 35 is: set4s[count] = new Set4(); (line 29 below)

SET4 CLASS:




NB I cannot take credit for the logic in the nested loops, I give credit to the webpage below.



Many thanks,
 
Greg Charles
Sheriff
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not exactly clear what you're trying to do, but your problem comes on line 32, where you reference set4s[1]. The first time through that loop, count will be 1, and on line 28 you create set4s as an array with "count" members. Since Java array indexes are 0-based, the only valid reference is set4s[0]. In any other iteration, that would be OK since count would be greater than 1, but even then you would be creating an array and then only using one member of it. Then on line 40, you store the new object in the TreeMap, but always with the same key, which replaces what was there before. So not only do you have an array from which you only use one member, you have a TreeMap that only ever contains one object. In other words, something is wonky. Maybe the code you borrowed was bad to begin with, or maybe you corrupted it as you brought it over.

Also the problem noted at line 28 is not actually a problem. Variables declared inside a block only have scope within that block. That is, the next time you come through the loop, it's fine to declare the variable with the same name, since the old one will be out of scope already.
 
dennis deems
Ranch Hand
Posts: 808
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You never need an inner class. You can't do anything with one that you wouldn't be able to do with a top-level class. Sometimes it is advisable to use an inner class, purely as a way of communicating your intent and keeping your code well-organized. When you have a discrete task that is logically separate from the rest of the enclosing class, yet relates specifically to it and is unlikely to be useful anywhere else, that's a candidate for an inner class.
 
Nigel Shrin
Ranch Hand
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Greg, that sorted it.

Aside: The treemap entry of "OrderedResults.put(1, set4s[count-1]);" is ok, as there can only be one entry that meets those criteria and I am after a specific sort order with that one first.

Thanks for your help!

 
Nigel Shrin
Ranch Hand
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dennis,

Inner classes, I've read about them and will have to bite the bullet soon! I guess they become essential for GUI and polling for state changes, such as a file watcher service.

Many thanks,
 
Campbell Ritchie
Sheriff
Pie
Posts: 47250
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have changed the red text to black and removed some space in the original post, because too much spacing and red text can be difficult to read.
 
Nigel Shrin
Ranch Hand
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Campbell, I'll try and format better in future
Nigel
 
Campbell Ritchie
Sheriff
Pie
Posts: 47250
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you really using a Set? If you add the same number several times, you only get one element.
 
Nigel Shrin
Ranch Hand
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Campbell, an oversight!
Nigel
 
Campbell Ritchie
Sheriff
Pie
Posts: 47250
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
Greg Charles
Sheriff
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nigel Shrin wrote:Thanks Greg, that sorted it.

Aside: The treemap entry of "OrderedResults.put(1, set4s[count-1]);" is ok, as there can only be one entry that meets those criteria and I am after a specific sort order with that one first.

Thanks for your help!



Good, I'm glad you got that working! I guess it's good anyway. You still seem to be ignoring the fact that you are creating an array, then only using one member of it. Also, I don't understand your being "ok" with replacing the single element in your tree map with every iteration. Do you realize that's what you're doing? Sort order and first don't really come into it as a one element set is always sorted, and its single element is always first.
 
Nigel Shrin
Ranch Hand
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Greg, I've rewrittten that whole section now!
I was going to change the "1" key to a variable after adding the first element, but it's all changed now.

thanks for your help
Nigel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic