Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

JoSQL

Name: Anonymous 2011-02-22 18:04

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?

Name: Anonymous 2011-02-22 18:06

Java? C++?

Name: Anonymous 2011-02-22 18:08

Oh sorry, Java.

Name: Anonymous 2011-02-22 18:09

oh god this is depressing. how to brainwash a young programmer into making 10 classes just to do a simple loop.

Name: Anonymous 2011-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: Anonymous 2011-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.

----------------------------
1: http://download.oracle.com/javase/1.5.0/docs/api/java/util/Iterator.html

Name: Anonymous 2011-02-22 18:27

I'm pretty stuck.

In the same program, I started to write this

 public class MyClass implements Iterator<int>
    {

jesus that's sad

Name: Anonymous 2011-02-22 21:03

>>7
You still interested in the solution? I went to sleep for a while, sorry...

Name: Anonymous 2011-02-23 1:52

What is the point of 'iterator'? Doesn't a for loop all you need?

Name: Anonymous 2011-02-23 1:54

What is the point of ``iterator''? Doesn't a map/filter/append all you need?

Name: Anonymous 2011-02-23 1:56

Doesn't a for loop all you need?

Stopped reading right there.

Name: Anonymous 2011-02-23 2:03

>>11
Congratulations, you stopped reading at the end of the sentence.

Name: >>8 2011-02-23 2:57

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.

Name: Anonymous 2011-02-23 3:08

>>13
It's his homework, he shouldn't ask for help in first place. Take it easy, >>13-kun.

Name: Anonymous 2011-02-23 5:04

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))

;;; Test
CL-USER> (prune-under-100 (make-random-list 10 900))
(741 297 647 229 678 123 389 681 363)


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).

Name: Anonymous 2011-02-23 9:49

Thanks for everyone's help anyway

Name: Anonymous 2011-02-23 10:39

>>16
OP here

no problem

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List