This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
hi, i need an "array" of int values, i don't know how many elements it will have but i also need to calculate the average value.
what should i choose an int or ArrayList()? if i coose ArrayList i have to do the calc by by self, i cant use array.avg() method. On the other side if i choose array it have to be ((un)necessary) big enough.
What is the best way to do that considering the performance and memory usage?
Firstly, there is no "array.avg()" method, so either way you will have to do the calculation yourself.
Secondly, if you used an array there is no guarantee that it will always be able to hold all your values, unless you know the maximum possible number of values, which I assume you don't.
Joined: Sep 14, 2005
so i implemented it like below but i was wandering can't this (calc the avg)be done a bit more simply? im movin from php to java and all this casting ArrayList,IntegerObject ..etc. looks for me, like to much work for that simple Task. Is it just an unexperienceed-me or there is a better way? How would u do this?
If you are using Java 1.5, you don't need all the casts. You can do something like "lst.get(i).intValue()". This uses the generics feature of Java 1.5. Even if you are using an earlier version, I wouldn't use toArray() because it has a few caveats. The main one is that your cast will cause a ClassCastException because toArray() returns an array of Objects. But an Integer "is a" Object, you might say. Unfortunately, toArray() creates each element in the array as an Object, so the polymorphic type information is missing. The elements of the resulting array are actually Objects, not Integers. So to keep things simple, you should just do "((Integer) lst.get(i)).intValue()" and not even mess with toArray.
With that said, I have a question about your design: Why does calkAvg() take a String argument? I can see that you are adding the number to the ArrayList. In my opinion, this means that calkAvg() is doing two separate tasks which can be separated into two different methods. I think it would make more sense to have one method called addNumberToList() (or something similar) that takes a String argument. Better yet, it should take an int or Integer argument so that it is also not doing too much work. Then you can have calkAvg() take no arguments. This will allow the caller to decide whether or not they want the average after each number is added. In other words, you can add a lot of numbers before calculating the average of all of them.
that is very nice to know, but I�m using the java 1.4
With that said, I have a question about your design: Why does calkAvg() take a String argument?
this is cause i have couple of other observers which r just ok with string. Ok that is maybe the next question for the observer pattern? What to do if i have 2 observers that takes(would be more suitable to take) an "int" and three that takes a "string"? My decision was- 3 for string, 2 for int- OK 3 wins U have better solution for that decision making?
this means that calkAvg() is doing two separate tasks which can be separated into two different methods.
yes, u r right again, i'll do that, this is just a draft to help me easy going . tnx again
Joined: Dec 06, 2001
When it comes to design, there is rarely ever a "right" or "wrong" answer. Some design choices make more sense over others, but often many different choices are equally viable. That's why I asked you to justify this particular design.
I still think the calkAvg() method does too much work. One possible way to split this up is to create two more methods:
addNumber(int num) addNumber(String num)
The version that takes an int will simply add it to the list. The version that takes a String will assume that the String contains digits and try to convert it into an int and then add the int to the list (perhaps by calling the int version to reduce code duplication).
Then calkAvg() can do just what its name says: calculate the average of the numbers in the current list.
Notice that each method does one specific task, and does it well. This is usually a good way to design because it makes it easier to change things.
I realize this might break your current design, though. I'm not entirely sure why you are referring to this class as an Observer (or is this the Observable). Of course, that's mostly because you haven't discussed the context of how this fits into the rest of your program. If you want to discuss it further, feel free to, but there's no pressure.
Feel free to take my comments for the grain of salt that they are worth.