CS62 - Fall 2026 - Class 8
Example code in this lecture
CommandLine
VS
LinkedList
Lecture notes
admin
- Quiz 3 recap
- Darwin due on Tuesday
command-line arguments
- Eclipse is an IDE (interactive development environment)
- it does a lot of things for you including syntax highlighting, autogeneration of some code, debugging, etc.
- one of the other things it does for you is compile your code and then run it
- Java is a compiled language
- we first compile the code
- this checks the syntax of your code
- and generates a runnable version of your code (.class file in the class of Java)
- we can then run the code once it is compiled
- Compiler
- The compiler checks the syntax and turns the code into a more machine-readable format
- In some language (e.g., C/C++) this machine-readable format is assembly code
- The challenge: this is specific to a particular processor, really to a particular "architecture"
- Java compiles to an intermediary language called "byte code"
- The byte code is then run on the Java Virtual Machine
- The nice thing about this is that the compiled code can run on any computer that has a Java Virtual Machine installed *without* having to be compiled for that specific computer
- We can compile Java from the command line (e.g., Terminal)
- cd into the directory where either the .java file exists (if no package) or the top level package exists
- if no package:
javac MyFile.java
- if package
javac package_name/MyFile.java
- you can do multiple java files with *
- e.g., we can compile all of the files in a package as:
javac package_name/*.java
- Once compiled, you can run them:
- java MyFile
- java package_name.MyFile
- when running a java program on the command-line, you can also pass arguments to the program (think, inputs) by adding extra information on the command-line
- java MyFile more information
- any text after the name of the file get passed as arguments (i.e., the String[] args parameter) to your main method!
- the things are separated by a space so
java MyFile more information
- would pass two things: more, information
Look at the CommandLine class in
CommandLine code
- only a main method
- iterates through the String[] and prints it out
- if we run the code in
VS Code
it prints out 0
- we can call it from the command-line, though
$ javac commandline/CommandLine.java
$ java commandline.CommandLine
Length: 0
$ java commandline.CommandLine this is some stuff
Length: 4
0: this
1: is
2: some
3: stuff
- You can do this in
VS Code
too
- On the left, click the Run/Debug button (4th one down)
- Click the link for "create a launch.json file"
- Go back to the Java file you want to run and run it once
- Got back to the launch.json file and you should see an entry for that particular class, something like:
{
"type": "java",
"name": "CommandLine",
"request": "launch",
"mainClass": "commandline.CommandLine",
"projectName": "CommandLine_b2c1112d"
}
- Add an entry to the end of this that lists the arguments:
{
"type": "java",
"name": "CommandLine",
"request": "launch",
"mainClass": "commandline.CommandLine",
"projectName": "CommandLine_b2c1112d",
"args": ["these", "are", "the", "arguments"]
}
The general ArrayList class implements the List interface (
http://docs.oracle.com/javase/7/docs/api/java/util/List.html
)
- Which of the following methods are performed quickly by the ArrayList class (quickly being sub-linear)? Slowly?
- get
- set the value of an existing element
- add to the end
- contains/indexOf
- add at an index
- remove an element
- size
- Quick:
- get
- set
- add to the end (like we saw last time... amortized)
- size
- Slow:
- adding an element at a particular index
- have to shift everything after towards the end
- removing an element
- have to remove and shift everything after towards the front
- contains
- have to iterate over every item
No free lunch!
- there's no one best data structure
- to make certain operations fast we often have to compromise and make other operations slower
- depending on what operations are important, one data structure may be more appropriate than another
- we're going to start looking at different data structures that are good at different things
Linked lists
- linked lists are a *recursive* data structure
- the data structure is built out of nodes
- a node stores both a data item as well as a reference to another node
- draw a picture and compare and contrast a picture of an array
- how could we do this using a class?
- look at the Node class in
LinkedList code
- we'll use generics again to make the class general purpose
- two things that we keep track of (with private instance variables)
- private E data: the data that we're actually storing in the node
- private Node next: a link to the next node in our linked list
- we have methods that allow us to access and manipulate the data within the node
- as well as methods that allow us to traverse and manipulate the links
- We want to build a linked list data structure that supports similar operations to ArrayList (i.e., the List interface). How would we do it?
- we'll just store a link the head of the linked list
- all of our operations will start at the head and then modify/traverse the list appropriately
- look at the LinkedList class in the
LinkedList code
- only keep track of a single node, the head of the list
- Look at the two addFirst methods in
LinkedList code
- addFirst
- create the new node
- set the next value of that new node to head
- set head to be the new node
- addFirst2
- does the same things, but utilizing the Node constructor
- when we evaluate the right hand side head gets passed as the next parameter to the Node
- we then just store this new node directory as the head
- how would we remove the first element (assuming that there is at least one element)?
- look at removeFirst in
LinkedList code
- save the value of head so we can return it
- set head to be head.next()
- return the value of the original head
To the slides! (Below are a few additional notes that are mostly also contained in the slides)
iterating through linked lists
- how can we iterate through all of the values in a linked list?
- how do we know were the end of the list is?
- it will be a node whose next value is null
- make a variable (it's often called "finger" and then move it down the list until we fall off of the end)
Node finger = head;
while (finger != null ) {
// do something with the current node, finger
finger = finger.next();
}
- do it recursively:
void iterate(Node current){
if( current == null ){
return null;
} else {
// do something with current
iterate(current.next());
}
}
write a method contains(E value). Returns true if the linked list has that value (based on .equals) and false otherwise
- look at contains in
LinkedList code
- start at the head and traverse through the nodes
- key: check if we've seen the node
- stop when we either find it or we run off the end
- at the end, see if we either found it or if we fell off the end of the list
- look at recursiveContains in
LinkedList code
- recursive data structures are often conducive to recursive method
- start at the head
- two base cases: we're either null or we've found it
- if neither of this, look at the next eleement
Which of these methods are fast/slow for singly linked lists?
- add to the end (slow)
- add to the front (fast)
- contains (slow)
- get (slow)
- insert at an index
- remove an element
- set the value of an existing element
- size
What does the singly linked list buy us?
- add and remove at the front of the list in constant time!
- what is the downside (no free lunch!)?
- get and set are now linear time operations