In CSC 108 you learned to think like a machine: well done! Valuable skill. But we want to work at high level / abstractly / humanly where possible. And be able to move between the levels. Especially important because different programming languages and different libraries for a particular programming language actually provide different low-level and high-level facilities. So programming is really the skill of moving between them. (Actually, there is a somewhat universal high-level way that human beings communicate algorithms/recipes/procedures, and good programmers think that way and then break it down into the particular facilities of the languages and libraries they have available). Lists ===== One definition: A list is a bunch of things (elements), in some sequential order. Notice: no mention of indices, pointers, how they're stored, etc. Another definition: A list is empty, or a thing (first element) and a list (rest of the elements). Notice: self-referential (recursive) and more self-contained (not built on concepts like "bunch" or "order"). Lists in Java API ================= We reviewed a lot of 108 that you need to be very comfortable with for 148. I posed a number of questions which we answered, verbally, on the board and with some diagrams. Below are mainly the questions with some reminders to get you started reviewing 108 (we can't cram a whole term of learning into this lecture note!). What's in the Java API? What's a class? What are the two main things you do with a class? make instances/objects call instance methods different ones can have different state call static/class methods Why not only instances? Why not only static methods? In java.util, various kinds of lists: ArrayList, LinkedList, Vector. What does "in java.util" mean? How does that affect our code? ArrayList l = new ArrayList(); How many `things' in memory did we create? What's the difference between primitive and reference types? How do we visualize variables, values, references/addresses, objects? l.add("Hi"); l.add("Bye"); System.out.println(l.size()); // prints: 2 System.out.println(l.get(0)); // prints: Hi Where do we visualize the methods? Can we deduce the API (in particular the method headers) from examples of its use? public void add(Object) // "void" is actually wrong, but best guess from above Why do we think it's Object and not String? What is the relationship between Object and String? What is polymorphism? public int size() Best guess, though perhaps l.size() produced an object whose toString returned "2"! What are the two reasons to call a method? return value side-effects What in the method header tells you which reason it's used? Can you always tell from the header? Can both reasons apply? Let's print all the elements of an ArrayList l. Low-level 108 style: for (int i = 0; i < l.size(); ++i) { Object o = l.get(i); System.out.println(o); } Better would be not to make a temporary o, but I put it there to compare with the loop below. But worst part is the index: our specification (print all the elements) says nothing about index numbers. If someone gives you a real-life list to read out, you don't make a temporary variable and number the elements in the list! You don't name the elements either. Java 5 (aka 1.5: blame the dual numbering on marketers at Sun Microsystems) has a non-indexing for-loop for list-like objects: for (Object o: l) { System.out.println(o); } Of course if we want to do this a lot, we make a method that just takes a list, and the *user* of the method doesn't think about temporary names for indices or elements. What to do next =============== Go back and review 108 so everything above (except the new for-loop) is clear. If you need specific guidance, one approach would be to go through previous 108 exams. Especially important are the memory and mental diagrams/models, since when programmers communicate (e.g. in 148 when we look at actual source code) they are really talking about those models! Think about what you do when you read the following line of `English code': Alice lives in a red house, on the second floor and has a pet cat. Do you `see' the red house, the floors and the cat? If someone asks you a few minutes later about Alice, do you remember the above sentence exactly, parse it and produce the answer by rearranging the words? Of course not. Well, maybe for one sentence, but not a whole paragraph or entire story: that would be incredibly hard! Instead you make a mental model and work with that. In these terms we could say: you at least need to have mastered `Java sentences' (single line statements) in 108, so that you can read one and instantly see what it does in isolation. Of course you put multiple statements together in 108 as well to make methods, classes and programs. That's the level we need to start at in 148. Whenever you have trouble with something that seems new in 148, make sure to first check whether you understand the implicit 108 aspects. Here's an analogy for those of you familiar with the TV show House: half the plots are based on the fact that diagnosing two diseases at once is *many* times harder than diagnosing one disease. I see this a lot in later year courses: a student thinks they have trouble with the new material, but it's trouble with older material that's the main problem. If you finish you're review, experiment with the ArrayList API: you know how to read an API, and since there are thousands of classes in the standard API, and many more in the rest of the world of Java being produced every day, you need to be able take any class API you haven't seen before and write code to try it out.