/* * Kyle Pierce * CSE 143 * * Client code for the ArrayIntList class */ public class ArrayIntListClient { public static void main(String[] args) { ArrayIntList list1 = new ArrayIntList(); ArrayIntList list2 = new ArrayIntList(); // how big should list1 and list2 be? 0 // how big are they really? 100 // this worked fine, but it is really inconvenient // no one will use your class if you make them do this list1.elementData[0] = 11; list1.elementData[1] = -2; list1.elementData[2] = 1000; list1.size = 3; // we write an add method to make things easier list1.add(11); list1.add(-2); list1.add(1000); System.out.println(list1); // (malicious) clients can still reach in and mess with our fields! // we can make them 'private' to stop this code from compiling list1.size = -5; // now we can't see how big the list is, though, so clients can // no longer write loops like this: for (int i = 0; i < list1.size; i++) { // some code goes here } // we add a size() method to allow clients to see (but not modify) the size // note that this code does not compile since you cannot assign things to // the result of calling a method, so our size field is safe list1.size() = -5; // we can, however, do things like this now for (int i = 0; i < list1.size(); i++) { // some code goes here } } }