Recitation 2 James Wei Professor Peck 1/17/2013. Covered in this Recitation Arrays Variable Scoping...

Preview:

Citation preview

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

Arrayspublic int uniqueCount(String[] list) {

Arrays.sort(list); // sort array in alphabetical orderString 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 Variablespublic class Frequencies {

public static int uniqueCount(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) {int count = 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 (int i=0; i<someArray.length; i++) {int j = 1;// some code

}// the line below is invalid, i and j are accessed out of scopeSystem.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 int printValue() {

System.out.println(“Value is “ + value);}public void changeValue(int newValue) {

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.

list Empty ArrayLis

t

Memory (Heap)

otherList

Referencespublic class Rec2Example {

private int myA;private int myB; 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…