[Newbies] Array questions

Jerome Peace peace_the_dreamer at yahoo.com
Fri Apr 23 22:48:46 UTC 2010



--- On Fri, 4/23/10, noobie <pntball650 at aol.com> wrote:

> From: noobie <pntball650 at aol.com>
> Subject: [Newbies] Array questions
> To: beginners at lists.squeakfoundation.org
> Date: Friday, April 23, 2010, 6:07 PM
> 
> I have a few questions about writing methods dealing with
> arrays....
> 
> Firstly, how would i make a method that returns true or
> false if the
> receiver is in the array.
> example
> 1 inArray: #(1 2 3 4)
> should return to true

inArray: theArray

^theArray includes: self .


> 
> Second, how would i make a method that will return the
> number of integers in
> the array different from the integer parameter
> 
> anArray howmany: 5   should return how many
> elements in the array are
> different from the number 

howMany: anItem

^(self reject: [ :each |
   each = anItem ]) size .

change reject to select to get the number that are the item.

You are asking very elementry questions here. You may be ready to read some of the literature.

I highly recommend Kent Becks book on Smalltalk.
Smalltalk Best Practice Patterns by Kent Beck
e.g.:
http://www.amazon.com/Smalltalk-Best-Practice-Patterns-Kent/dp/013476904X/ref=sr_1_1/184-7387794-7351534?ie=UTF8&s=books&qid=1272062516&sr=8-1

> thanks again.
> -- 



      


More information about the Beginners mailing list