1 / 12

Recitation 2

Recitation 2. James Wei Professor Peck 1 /17/2013. Covered in this Recitation. Arrays Variable Scoping Local variables Instance variables Class variables References. http://goo.gl/LWL0xi L W L (zero) x i. Arrays. public int uniqueCount (String[] list) {

zaynah
Download Presentation

Recitation 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Recitation 2 James Wei Professor Peck 1/17/2013

  2. Covered in this Recitation • Arrays • Variable Scoping • Local variables • Instance variables • Class variables • References http://goo.gl/LWL0xi L W L (zero) x i

  3. Arrays public intuniqueCount(String[] list) { Arrays.sort(list); // sort array in alphabetical order String last = list[0]; int count = 1; // Invariant: count is number of unique words in list[0…k] for (int k=1; k<list.length; k++) { if (!list[k].equals(last)) { last = list[k]; count++; } } } • Answer the “Array” questions on the form

  4. Variable Scoping • All variables have scope - the areas where the variable is accessible. • Variables exist within their scope and are not visible outside of it. • Typically can see the scope of a variable by looking at the block of code it is defined in. • method, class, for loop, while loop, etc. • Three types to cover: • Local variables • Instance variables • Class variables

  5. Local Variables public class Frequencies { public static intuniqueCount(String[] list) { Arrays.sort(list); String last = list[0]; for (int k=1; k<list.length; k++) { if (!list[k].equals(last)) { last = list[k]; count++; } } return count; public static void main(String[] args) { intcount = 1; String[] list = {"apple", "banana", "apple", "pear"}; uniqueCount(list); } } Answer the “Scope” questions

  6. Local Variables • You can identify a variable’s scope by checking the first pair of curly braces surrounding its declaration. Usually this will give you the block of code pertaining to its scope. • for-loop example for (inti=0; i<someArray.length; i++) { int j = 1; // some code } // the line below is invalid, i and j are accessed out of scope System.out.println(“i=“ + i + “, j=“ + j);

  7. Instance Variables • Instance variables have a scope of an entire instance of a class. • Example: public class Node { private int value; public intprintValue() { System.out.println(“Value is “ + value); } public void changeValue(intnewValue) { value = newValue; } } • Notice that the variable “value” is declared outside of any methods in the Node class, but inside the class itself. This indicates that its scope is the entire class itself.

  8. References • Java accesses objects by reference • ArrayList<String> list = new ArrayList<>(); creates: • To access variable, look up list variable • points to ArrayList object in memory. “list” is a reference to the ArrayList object. • ArrayList<String> otherList = list; gives the following: • We have TWO references, “list” and “otherList”, • both reference the same object in memory! • Changing “list” will also change “otherList”, and vica versa. Memory (Heap) Empty ArrayList list otherList

  9. References public class Rec2Example { private intmyA; private intmyB; public Rec2Example(int a, int b) { myA = a; myB = b; } public void setA(int a) { myA = a; } public static void main(String[] args) { Rec2Example ex = new Rec2Example(33, 7); Rec2Example ex2 = ex; ex.setA(44); } }

  10. References public static void randomFunction() { ArrayList<Integer> list = new ArrayList<>(); ArrayList<Integer> otherList = list; list.add(44); otherList.add(55); }

  11. References • Just remember, Java remembers which objects are in memory by keeping references that point to those objects, and these references are assigned whenever you set something “=“ to something else. • You will learn more about how this works under the covers in CS250 if you take it…

  12. Have a good weekend! Make sure to submit your answers before next Friday to get credit for today…

More Related