Logout

## Collections, a While Loop Focus

Here's another full example of a collection/list. But with this one, think back to the way that while loops are supposed to work, and appreciate why they work well with collections, whose size is often unknown until runtime.

```14
15   public static void main(String[] args) {
16       OurList<Integer> originalList = new OurList<Integer>();
21
22       int numberNumbers = (int)(Math.random()*20 + 10); //so randomly 10 to 30 in size
23       for(int i = 0; i < numberNumbers; i++){
24           int num = (int)(Math.random()*100 - 50);
26       }
27       OurList<Integer> positiveEvenList = new OurList<Integer>();
28       OurList<Integer> positiveOddList = new OurList<Integer>();
30       while(originalList.hasNext()){
31           int x = originalList.getNext();
32           if(x > 0){
33               if(x % 2 == 0){
37               }else{
39               }
40           }
41       }
42       //print out the positive odd numbers
43       if(!positiveEvenList.isEmpty()){
44           System.out.print("Positive Even Numbers: ");
45           positiveEvenList.resetNext();
46           while(positiveEvenList.hasNext()){
47               System.out.print(positiveEvenList.getNext() + " ");
48           }
49       }else{
49           System.out.prinln("Positive even list is empty.");
50       }
50       System.out.println();
51       if(!positiveOddList.isEmpty()){
52           System.out.print("Positive Odd Numbers: ");
53           positiveOddList.resetNext();
54           while(positiveOddList.hasNext()){
55               System.out.print(positiveOddList.getNext() + " ");
56           }
57       }else{
57           System.out.prinln("Positive odd list is empty.");
58       }
58   }```