Logout

Collections to and from Arrays

An example of going from an array to a list.

```
Make an array of 365 daily temperatures ranging from 16 to 40 degrees Celsius.
From it, make two lists, one of temperatures less than 20, and one temperatures
more than 37.

For this example, just have the lists be global, and in another method print out
the chilly and hot temps.
But make another program in which you send the lists as arguments to a methods
which print out the chilly, and hot temperatures.

38
39    static OurList<Integer> chillyTemps = new OurList<Integer>();
40    static OurList<Integer> hotTemps = new OurList<Integer>();
41
42    public static void main(String[] args) {
43
44        int [] allTemps = new int[365];
45        for(int i = 0; i < allTemps.length; i++){
46            allTemps[i] = (int)(Math.random() * 24 + 16);
46        }
47        for(int i = 0; i < allTemps.length; i++){
47           if(allTemps[i] < 20){
49           }else if(allTemps[i] > 37){
51           }
52        }
53        printOutChillyAndHotTemps();
54
55    }
56    public static void printOutChillyAndHotTemps(){
57        System.out.println("Chilliest temps:");
58        chillyTemps.resetNext(); //even though not necessary, a good habit
59        if(!chillyTemps.ourIsEmpty()){
60            while(chillyTemps.hasNext()){
61                System.out.print(chillyTemps.getNext() + " ");
62            }
63        }
64        System.out.println();
65        System.out.println("Hotttt temps:");
66        hotTemps.resetNext();
67        if(!hotTemps.ourIsEmpty()){
68            while(hotTemps.hasNext()){
69                System.out.print(hotTemps.getNext() + " ");
70            }
71        }
72    }```

Output is random, but something like:

16 19 17 17 19 19 17 16...

38 39 39 39 38 39 38 38...

At some point, do another example going the other way; from a list to an array.