So I have to create a program that will populate an array list with randomly generated values(already did). Next I have to write an iterator that will traverse the array list and keep only the numbers above 100. Do I create a new class to write this code in or do I keep it in the same class?
oh god this is depressing. how to brainwash a young programmer into making 10 classes just to do a simple loop.
Name:
Anonymous2011-02-22 18:16
I know I know. The real problem is my iterator.
I have
Iterator q = al.iterator(); al = ArrayList
So I need the iterator to go through the array, so I write a for loop, correct?
for(q = 0; q > al.size(); q++)
Is that ^^ on the right track?
Name:
Anonymous2011-02-22 18:19
>>1
I would assume that you have to implement iterator interface1 in a separate class (which implements Iterator<int>). The class' constructor should take an ArrayList<int> as an argument. If you are still stuck, re-bump this thread and I'll write you an implementation.
Don't you just hate it when they ask a question, then get impatient within a few minutes and leave? It's oh so much more annoying on IRC. Fucking shit.
No idea about the language you want to write this in OP, but here's a simple CL implementation:
(defun make-random-list (n &optional (limit most-positive-fixnum)) (loop repeat n collect (random limit)))
;; utility
(defun rcurry (function &rest args)
(lambda (&rest more-args)
(apply function (append more-args args))))
(defun prune-under-100 (list) (remove-if (rcurry #'< 100) list))
Of course, I think this itself is pretty silly and you could just generate random numbers in a range or just something like:
(defun random-in-range (n m) (+ n (random (1+ (- m n)))))
(defun make-randomized-list (n count) (loop repeat count collect (random-in-range 0 n)))
;;; Test
CL-USER> (make-randomized-list 9000 10)
(3864 2366 3282 7329 2284 4068 230 3419 8077 3429)
Again, very simple, some other simple variations:
CL-USER> (loop repeat 10 collect (+ 100 (random 900)))
(275 497 303 233 253 315 304 819 492 525)
;;; or
CL-USER> (mapcar (curry #'+ 100) (loop repeat 10 collect (random 900)))
(379 180 931 680 487 669 266 625 914 753)
As you can see, all this stuff is very very trivial, why do you need classes? Even in C, it's only one or two lines of code (a for loop initializing some array to random values + 100).