List
interface is implemented by two distinct concrete classes:ArrayList
: backed by an array.LinkedList
: backed by a chain of “nodes”.ArrayList
and LinkedList
depends on algorithmic complexity.Declaring and creating a list:
ArrayList<Integer> lst = new ArrayList<>();
To sort Robots, we have to define an ordering.
We could choose alphabetic by Name.
We could choose chronological by Model.
Or by cost, intelligence, distance, power, anything!
public class Robot {
private String name;
private int model;
// plus other stuff for robots.
}
//file: Robot.java
Comparator
is a simple interface with one key method.
Each object instance encapsulates a comparison method.
These are used by sorting algorithms to sort data.
public interface Comparator<T> {
public int compare(T o1, T o2);
}
class MyRobotComparator implements Comparator<Robot> {
public int compare(Robot o1, Robot o2) {
return o1.name.compareTo(o2.name);
}
}
class MyRobotComparator implements Comparator<Robot> {
public int compare(Robot o1, Robot o2) {
return o2.model - o1.model;
}
}
<etype>
public interface List<E> {
void add(E x);
Iterator<E> iterator();
}
public interface Iterator<E> {
E next();
boolean hasNext();
}
public interface List<Student> {
void add(Student elt);
void set(int index, Student elt);
Student get(int index);
}
public class Roster {
protected List<Student> students;
public Student getStudent(int index) {
return students.get(index);
}
}
//Compiler translates interface to this:
public interface List<E> {
void add(E elt);
void set(int index, E elt);
E get(index);
}
//Used like this:
Generics are related to polymorphism; however, they are distinct!
Polymorphism is when we refer to objects of different classes by the type of a common parent. (E.g., Rectangle and Circle are Shapes.)
But when we pull an item out of a collection, we would only know the parent class, not the "real" class.
Generics let the compiler keep track of this for us.
// Polymorphic but Not Generic
public interface List {
void add(Object x);
void set(int index, Object value);
Object get(int index);
}
// Can lead to ambiguous looking code:
List a = new LinkedList();
a.add(2);
a.add(3);
a.add(5);
...
List b = new LinkedList();
b.add("Cat");
b.add("Dog");
...
Object x = a.get(0);
Object y = b.get(0);
// Compare to using generics:
List<Integer> a = new LinkedList<>();
a.add(2);
a.add(3);
a.add(5);
...
List<String> b = new LinkedList<>();
b.add("Cat");
b.add("Dog");
...
int x = a.get(0);
String y = b.get(0);
ls -l /
command on a Macbook, showing the contents of the root directory.ls -l /
command on a Fedora system, showing the contents of the root directory./Users/adam/Documents/teaching/courses/171/Fall2021/Workshops/W1.pdf
/Users/adam/eclipse-workspace/fileio/src/FileInputDemo.java
FileInputDemo.java
./fileio/src/FileInputDemo.java
The shell (aka terminal) is a program that reads commands and executes them, while maintaining some environment variables to keep the process organized.
Examples:
PATH
is a list of all directories searched when you run a command (such as javac).PWD
is the current working directory used for relative paths (this is also maintained by other processes, such as IntelliJ or Eclipse)Try this in Java:
Map<String, String> env = System.getenv();
argv
array in main.Example of using commandline arguments in Java:
// Output:
adam@dbmbp FilesExceptions % java CommandLineArgs red yellow blue "csc 171 too"
Here are the arguments:
0 is red
1 is yellow
2 is blue
3 is csc 171 too
// Usage example:
public class CommandLineArgs {
public static void main(String[] args) {
System.out.println("Here are the arguments:");
for (int n = 0; n < args.length; n++)
System.out.println(n + " is " + args[n]);
}
}
// CommandLineArgs.java
ls
, cd
, and pwd
are used to list and change directories.File
: access attributes but NOT the dataFileReader
, FileWriter
(both are Reader
, Writer
): access a file as a stream of characters.FileInputStream
, FileOutputStream
(InputStream
, OutputStream
): access a file as a stream of bytes.RandomAccessFile
: access a file however you like!File
: access attributes but NOT the dataFileReader
, FileWriter
(both are Reader
, Writer
): access a file as a stream of characters.BufferedReader
, BufferedWriter
: access a file efficiently.PrintWriter
: pretty print to a file.java.io.File
is the primary class for working with metadata, such as access times, permissions, and file names.exists()
, canRead()
, canWrite()
delete()
, renameTo()
isDirectory()
, isFile()
list()
, listFiles()
, mkdir()
, mkdirs()
lastModified()
, setLastModified()
, length()
File
class to inspect file metadata.read()
, reset()
, skip()
, close()
, write()
The familiar Scanner
class provides a constructor which reads from the FileReader
base class, which means you can easily use all your favorite Scanner methods when reading from a file.
Example:
FileReader fr = new FileReader("file.txt")
Scanner scnr = new Scanner(fr);
In the case of writing to files, we want to recreate the effects of calling System.out.println()
and System.out.printf()
.
The System.out
object is an instance of a PrintStream
, which prints a sequence of bytes. For printing text, we can connect a PrintWriter
to a FileWriter
.
Example:
FileWriter fw = new FileWriter("file.txt");
PrintWriter oWriter = new PrintWriter(fw);
oWriter.print("Hello, world!");
close()
your FileStreams
when you're finished with them — but if you forget, the garbage collector will (eventually) take care of it.