File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes array or ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "array or ArrayList" Watch "array or ArrayList" New topic
Author

array or ArrayList

Ner min
Ranch Hand

Joined: Sep 14, 2005
Posts: 76
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?

tia nermin
Stuart Gray
Ranch Hand

Joined: Apr 21, 2005
Posts: 410
A few things to consider:

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.
Ner min
Ranch Hand

Joined: Sep 14, 2005
Posts: 76
oh yes, there is no avg() in java, sorry i mixed javascript and java
hmmm, following: below is my inner class which is simply an observer class
which gets notyfied from caller with "update(String s)" method. All it does is displays one label with the avarage value of some data. ok, i now String is a bit inappropriate for arg-type but i have also other observers which r "happy" withs String arg-type.

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?

[ October 09, 2005: Message edited by: Ner min ]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
One simplification you could perform would be to
return Integer.toString(Math.round(sum/len)));

Integer.toString() is a static method which takes an int and returns a String. So you don't need an instance of an Integer to use it.
[ October 09, 2005: Message edited by: Marilyn de Queiroz ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Ner min
Ranch Hand

Joined: Sep 14, 2005
Posts: 76
tnx Marilyn,

that means i wasn't bad at all
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.

I hope this helps.

Layne


Java API Documentation
The Java Tutorial
Ner min
Ranch Hand

Joined: Sep 14, 2005
Posts: 76
tnx Layne,

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
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.

Layne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array or ArrayList
 
Similar Threads
double [][] array to arraylist
Array list/linking list
update a JList based on an Array
dynamic Array (ArrayList)
LinkedHashMap and LinkedList, iteration time doubt