120 likes | 286 Views
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) {
E N D
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 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
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
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
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);
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.
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
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); } }
References public static void randomFunction() { ArrayList<Integer> list = new ArrayList<>(); ArrayList<Integer> otherList = list; list.add(44); otherList.add(55); }
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…
Have a good weekend! Make sure to submit your answers before next Friday to get credit for today…