Java Help for CompSci 201

On this page we'll keep track of the Java primitive types, operators, library classes, and methods that we've covered in class. You should also review the online Java API and the Java Tutorial for more complete coverage.

.
Mathematical Operators
Symbol Meaning Example
+ addition 4 + 5 = 9
- subtraction 9 - 5 = 4
* multiplication 3*5 = 15
/ division 6/3 = 2
6/4 = 1
6.0/4 = 1.5
% mod/remainder 5 % 3 = 2
String Operators
+ concatenation "ab"+"cd"="abcd"
Primitive Comparison Operators
== is equal to 3 == 3 is true
!= is not equal to 3 != 3 is false
>= is greater than or equal to 4 >= 3 is true
<= is less than or equal to 4 <= 3 is false
> is strictly greater than 4 > 3 is true
< is strictly less than 3 < 3 is false
Boolean Operators
x=5
! flips the value of a boolean !(x == 5) is false
&& returns true only if both parts of it are true (x > 3 && x < 7) is true
(x > 3 and x > 7) is false
|| returns true if at least one part of it is true (x < 3 || x > 7) is false
(x < 3 || x < 7) is true
Object Comparison Methods
equals(Object other) indicates whether some other object is "equal to" this one
compareTo(Object other) Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified other object.
compare(Object o1, Object o2) Compares its two arguments for order.
String Methods
s="colorful"
Name Returns Example
.indexOf(String str) index of first occurrence s.indexOf("o") == 1
s.indexOf("e") == -1
.indexOf(String str, int fromIndex) index of first occurrence starting at the specified indexs.indexOf("o",2) == 3
.substring(int beginIndex, int endIndex) Returns a new string that begins at the specified beginIndex and extends to the character at index endIndex - 1s.indexOf("1,3").equals("ol")
.trim() copy with leading/trailing whitespace removed "   big ".trim().equals("big")
.split(String regex) Splits this string around matches of the given regular expression and return an array. "big bad dog".split(" ") is {"big","bad", "dog"}
"this,old,man".split(",") is {"this", "old", "man"}
In general can split on any other strings, not just a comma, e.g., s.split(":") will split on a colon, s.plot("\\s+") will split on all white space, and s.split("gat") will split on the string "gat".
.startsWith(String str) boolean if starts with string s.startsWith("color") == true
s.startsWith("cool") == false
Type Conversion Functions
(int)x turn x into an int value if x is a double, long, char, etc. (int) 123.99 == 123
Integer.parseInt(String s) turns String into an int value. Integer.parseInt("1024") == 1024
Integer.toString(int n) turn n into a String Integer.toString(-70).equals("-70")
  Similar methods exist for double (Double), char (Character), long (Long)
Random Methods
Math.random() returns a random value in the range [0.0, 1.0)
nextInt(int n) Returns a random integer between 0 and n-1 inclusive. Random gen = new Random();
// produces a number between 0 and 9
gen.nextInt(10);
nextDouble() Returns a random real number between 0 and 1.
Scanner Methods
.clear() close the scanner
.hasNext() returns true if the scanner has another token
.hasNextDouble() returns true if the next token in the scanner's input can be interpreted as a double
.hasNextInt() returns true if the next token in the scanner's input can be interpreted as an int
.next() finds and returns the next complete token (string)
.next(pattern) returns the next token if it matches the specified pattern
.nextInt() returns the next token of the input as an int
.useDelimiter(pattern) sets the scanner's delimiting pattern to the specified pattern
ArrayList Methods
.add(element) add a specified element to the end of the list
.clear() removes all of the elements in the list
.contains(object) returns true if the list contains the specified object
.get(index) returns the element at the specified index of the list
.isEmpty() returns true if the list is empty
.remove(object) removes the first occurrence of the specified object from the list, if present
.size() returns the number of elements in the list
.toArray(T[] a) converts the ArrayList into an array, e.g. String[] array = list.toArray(new String[0]);
LinkedList Methods
.add(element) appends the element to the end of the list
.peek() retrieves but does not remove the head (first element) of the list
.poll() retrieves and removes the head of the list
Set Methods
.add(element) adds an element to the set if it is not already present
.addAll(Collection c) adds all of the elements of specified collection to the set if it is not already present
.contains(object) returns true if the set contains the specified object
.isEmpty() returns true if the set is empty
.remove(object) removes the specified object from the set if present
.removeAll(Collection c) removes from this set all of the elements from the specified collection if present
.retainAll(Collection c) Retains only the elements from the specified collection if present
Map Methods
.clear() Removes all mappings from this map.
.containsKey(key) returns true if this map contains a mapping for the specified key.
.containsValue(value) returns true if this map maps some key to the specified value.
.entrySet() returns a set of Map.Entry pairs (key and value).
.get(key) returns the value that the specified key is associated with
.isEmpty() returns true if the map is empty
.keySet() returns a set of the keys in the map
.put(key, value) associated the specified key with the specified value in the map
.remove(key) remove the mapping for this key
.size() returns the number of mappings
.values() returns a Collection of the values contained in the map
Collections Methods
Collections.sort(list) sorts the list in the ascending order
Collections.sort(list, comparator) sorts the list according to the order induced by the comparator (e.g., List lst; Collections.sort(lst, new Comparator c))
Queue Methods
.add(element) add a specified element to the queue
.peek() retrieves, but does not remove, the head (oldest element added) of this queue
.poll() retrieves and removes the head of this queue
Priority Queue
PriorityQueue() creates a PriorityQueue and orders its elements according to their natural ordering.
PriorityQueue(initialCapacity, Comparator) creates a PriorityQueue with the specificed initial capacity and orders its elements according to the comparator
.add(element) add a specified element to the queue
.clear() removes all the elements from the queue
.peek() retrieves, but does not remove, the head of this queue
.poll() retrieves and removes the head of this queue
.size() returns the number of elements in this queue
Stack Methods
.empty() tests if the stack is empty
.peek() retrieves, but does not remove, the top (element added most recently) of the stack
.pop() retrieves and removes the top of the stack
.push(item) pushes the item onto the top of the list

Last modified: Wed Aug 31 09:52:54 EDT 2011