| 1 | package common; |
| 2 | |
| 3 | public class IntList { |
| 4 | public int first; |
| 5 | public IntList rest; |
| 6 | |
| 7 | public IntList(int f, IntList r) { |
| 8 | first = f; |
| 9 | rest = r; |
| 10 | } |
| 11 | |
| 12 | /** Returns the ith item of this IntList. */ |
| 13 | public int get(int i) { |
| 14 | if (i == 0) { |
| 15 | return first; |
| 16 | } |
| 17 | return rest.get(i - 1); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Method to create an IntList from an argument list. |
| 22 | * You don't have to understand this code. We have it here |
| 23 | * because it's convenient with testing. It's used like this: |
| 24 | * <p> |
| 25 | * IntList myList = IntList.of(1, 2, 3, 4, 5); |
| 26 | * will create an IntList 1 -> 2 -> 3 -> 4 -> 5 -> null. |
| 27 | * <p> |
| 28 | * You can pass in any number of arguments to IntList.of and it will work: |
| 29 | * IntList mySmallerList = IntList.of(1, 4, 9); |
| 30 | */ |
| 31 | public static IntList of(int... argList) { |
| 32 | if (argList.length == 0) |
| 33 | return null; |
| 34 | int[] restList = new int[argList.length - 1]; |
| 35 | System.arraycopy(argList, 1, restList, 0, argList.length - 1); |
| 36 | return new IntList(argList[0], IntList.of(restList)); |
| 37 | } |
| 38 | |
| 39 | public boolean equals(Object other) { |
| 40 | if (other instanceof IntList oL) { |
| 41 | if (first != oL.first) { |
| 42 | return false; |
| 43 | } else if (rest == null && oL.rest == null) { |
| 44 | return true; |
| 45 | } else if (rest != null && oL.rest != null) { |
| 46 | return rest.equals(oL.rest); |
| 47 | } else { |
| 48 | return false; |
| 49 | } |
| 50 | } |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | public String print() { |
| 55 | if (rest == null) { |
| 56 | // Converts an Integer to a String! |
| 57 | return String.valueOf(first); |
| 58 | } else { |
| 59 | return first + " -> " + rest.print(); |
| 60 | } |
nothing calls this directly
no outgoing calls
no test coverage detected