/* * Kyle Pierce * CSE 143 * * Simple client program using the ListNode class */ public class ListNodeClient { public static void main(String[] args) { ListNode list = new ListNode(3, new ListNode(5, new ListNode(2))); int[] arr = {3, 5, 2}; // This is the way most programmers would write this code // and the way you will see us do it in lecture and section. ListNode current = list; while (current != null) { System.out.println(current.data); current = current.next; } // This is how we would have written such a loop with an array. for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } // This is another way. There's nothing necessarily wrong // with it, but when the loop conditions get longer and more // complicated this way can become hard to read. // // You'll also find that the scope of curr is inconvenient // since (as is true with the add method) you often need access // to curr after of the loop as well for (ListNode curr = list; curr != null; curr = curr.next) { System.out.println(curr.data); } } }