• Post Reply Bookmark Topic Watch Topic
  • New Topic

collection param problems  RSS feed

 
mike foulk
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a method that receives a collection in its parameters. Dosen't this mean that it should take an array in the method - method(array)?

an array is a collection, right?

Second question: how do I check a "Collection" for all/100% Number content. (make sure that it has no strings, nulls,

So far I'm using an- statement.

these aren't working so far.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, an array is not a Collection.

However, there is a method in java.util.Arrays called asList that will convert an array of object references to a List which is a Collection.

Can you control the content of the Collection? That is, if you only allow numeric content to the be added to the Collection, you won't have to make a check.
 
Eitan Levi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An array is not a Collection, and a Collection is not an Array. Collections do have a method called
toArray that will convert it from a Collection to an array.

I'm not sure what you been by "checking for all/100% Number content"... Are you saying that you want your Collection/Array to contain only integers, floats or doubles?
 
mike foulk
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Keith Lynn:


Can you control the content of the Collection? That is, if you only allow numeric content to the be added to the Collection, you won't have to make a check.

How do you only allow numeric content?
 
mike foulk
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Keith Lynn:


Can you control the content of the Collection? That is, if you only allow numeric content to the be added to the Collection, you won't have to make a check.




How do you only allow numeric content?
 
B Howard
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Typecast the collection to the Numeric type, something like


Then you should be able to do a simple test, assuming that you tested the class type to be


If you want to get the value try using something like


Does this help any? It seems like the other posters weren't realizing what you were asking.
 
B Howard
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry....switch numeric to number to make it work.
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Um, B Howard, I'm pretty sure that you'll never be able to cast a Collection to a Number.
 
Mike Noel
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I was thinking the same thing. I don't think Collection casts to a Number all that well.

I think the OP was asking about checking to see if each of the elements in the Collection are numeric. In that case the way to do this is to iterate over all of the elements in the collection and use "instanceof Number" for each one. Notice that Byte is a subclass of Number so you might have to filter out Bytes if you don't consider them numeric.

So the classes, interfaces, and methods involved in solving this problem are:

Collection (interface)
Collection.iterator (method)
Collection.hasNext (method)
Collection.next (method)
instanceof (operator)
Number (class)
Byte (class, if you need to filter out Bytes)

Sound good?
 
mike foulk
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, after make sure that the collection is totally of type number - how do I add them(the collection values) up and store them into a double or a int?

--

So having done this, getting an average of the total should be easy by dividing the total by colection.size() right?
 
Mike Noel
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will want to use the doubleValue() method on the Number class to sum these things up. I'm suggesting using doubles because that's the largest type that will hold all of the possible values. If you tried using int values and there was a Double in your collection then you might have a problem.

So, basically, after determining that each Object in the Collection is a Number, you will cast the Object to a Number and then call the doubleValue method on that object to get a native double value. Sum up all of those doubles. And, as you said, if you want the avg just divide by the total.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!