Download 02_LinkedLists

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
2A. MORE OOP IN JAVA
REFERENCE:
HTTP://DOCS.ORACLE.COM/JAVASE/TUTORIAL/JAVA/JAVAOO/ACCESSCONTROL.HTML
PUBLIC / PRIVATE / PROTECTED
• A way to control who can access parts of the class
• Why?
•
•
•
•
Good Design*
A way to "bullet-proof" your code.
Can make it easier to modify your code.
Jason says so 
• It'll be a bit aggravating at first…
• Basic idea:
• Put a modifier in front of a "thing"
•
•
•
•
An attribute (variable)
A method (function)
A nested class
…
• 4 modifiers: public, private, protected, [no modifier = packageprivate].
• 4 contexts: in-class, in-package, in-subclass, world
NESTED CLASSES
• Basically a class within a class.
• Why?
• Organization / Readability: we don't have to look in 2+ files.
• Of course…that one file can get really crowded…
• Encapsulation: We can hide the nested class from users of
the outer class (if we choose)
package nestedexample;
public class Outer
{
private class Inner
{
private float y;
Inner(float b)
{
y = b;
}
public String blah()
{
return "Bl_" + y + "_ah";
}
}
private Inner x;
public Outer(float c)
{
x = new Inner(c);
}
public void test()
{
System.out.println("Te_" + x.blah() + "_st");
}
public static void main(String[] args)
{
Outer o = new Outer(3.2f);
o.test();
}
}
NESTED
CLASS
EXAMPLE
CONTEXT EXAMPLES
// TheClass.java
package accessexample;
// OtherClass.java
package accessexample;
public class TheClass
{
public class OtherClass
{
In-class
Inpackage
}
import accessexample.*;
public class TestApp
{
public static void main(String[] args)
{
World
}
}
}
// OtherClass.java
import accessexample.TheClass;
public class OtherClass extends TheClass
{
In-subclass
}
This is the class of interest
ACCESS RULES
Can access?
Modifier
In-class
In-package
In-Subclass
World
public
Y
Y
Y
Y
protected
Y
Y
Y
N
no modifier (packageprivate)
Y
Y
N
N
private
Y
N
N
N
• [Do some examples within the classes on the last
slide]
2B. LINKED STRUCTURES
http://testplant.blogspot.com/2011_08_01_archive.html
OVERVIEW
• What are they?
• Comparisons to arrays
•
•
•
•
Heterogeneous vs. Homogeneous
Structure
Operations: add, get, remove, iteration
Efficiency:
• Time efficiency
• Memory efficiency
• Usage in other algorithms / data structures
REFERENCES (AND POINTERS)
• "basic" types vs. objects
• And the "class-wrapped" version of basic types
• use of new
• Simple Example:
REFERENCE EXAMPLE
public static void test1(int x)
{
x = 10;
}
public static void main(String[] args)
{
int a = 5;
test1(a);
System.out.println("a = " + a);
}
REFERENCE EXAMPLE, CONT.
public static void test2(int[] xL)
{
xL = new int[xL.length * 2];
for (int i = 0; i < xL.length; i++)
xL[i] = 10;
}
// Try it with and without this...
public static void print_array(String name, int[] L)
{
System.out.print(name + " = [");
for (int i = 0; i < L.length; i++)
{
if (i == 0)
System.out.print(L[0]);
else
System.out.print(", " + L[i]);
}
System.out.println("]");
}
public static void main(String[] args)
{
int[] b = new int[3];
b[0] = b[1] = b[2] = 5;
test2(b);
print_array("b", b);
int[] c = b;
c[0] = 99;
print_array("c", c);
print_array("b", b);
}
Do a memory diagram…
REFERENCES AND POINTERS
• A Java reference is like a C/C++ pointer
• (behind the scenes it likely is a C pointer)
• Differences
• Memory management (at least free / delete)
• More type safety
• Almost every variable in Java is a reference except:
• basic types (int, double, boolean)
• String
• …(there might be some more I'm not aware of )
BASIC STRUCTURE OF A LINKED LIST
• The linked list is made of Nodes. Each node
contains:
• A payload: The actual value we're storing
• A next reference (null if this is the last)
• [A prev reference] (for a doubly-linked list)
• The LinkedList itself contains:
• The size of the list
• We didn't need to store this in the IntList class – why here?
• A reference to the head of the list (or null if empty)
• [A reference to the tail of the list (or null if empty)
TRAVERSING A LINKED LIST
• Makes heavy use of references.
• Pseudo-code
Node temp = mHead;
// Note NOT new
while (temp != null)
{
// Do any operations / tests on temp
// Move to the next node
temp = temp.mNext;
}
ADDING TO A LINKED LIST
• Two major variants:
• At to end (or beginning)
• Add in order
• In all cases, we create a new Node (newN)
• Cases:
• Empty list
• set mHead (and mTail?) to newN
• Non-empty list, inserting at head (tail)
• Make newN point to the old head (tail)
• Update mHead (and / or mTail)
• Non-empty list, inserting elsewhere:
• Traversal until we reach the "right" spot
• Make temp.mNext point to newN
REMOVING ELEMENTS FROM A LINKED
LIST.
• Do a traversal to find the targetNode
• Cases:
• Empty list
• This is the same node as that pointed to by mHead or mTail
• This is a node in the middle of the list
• Do pseudo-code (& diagram) on board.
BUILT-IN LINKED LISTS
LinkedList L = new LinkedList();
L.add("abc");
L.add(15);
L.add(3.719);
Iterator I = L.iterator();
while (I.hasNext())
{
Object curValue = I.next();
System.out.println(curValue);
}
OTHER USES FOR LINKED LISTS
• Both (can) use a LinkedList implementation
• We just have a more limited form of adding / removing.
• Queues
• FIFO (First-in-first-out)
• Only add (push) to the tail and remove (pop) from the head.
• Uses:
• As a tool in our path-finding (breadth-first search) of a graph.
• Stack
• LIFO (Last-in-first-out)
• Only add (push) to the tail and remove (pop) from the tail (or
vice-versa).
• Uses:
• To optimize recursive functions.
• To do a depth-first traversal of a tree.