Ch3: Core Java APIs Flashcards
What does API stand for?
Application Programming Interface
What is the value of s1 and s2, and why?
String s1 = “1”;
String s2 = s1.concat(“2”);
s2.concat(“3”);
s1 = “1”
s2 = “12”
The String object is immutable, so the .concat method doesn’t modify the object.
What’s another name for the string pool?
The intern pool
What’s another name for the intern pool?
The string pool
What goes into the string pool?
String literals (not String objects created with “new”)
How do you get the number of characters in a String?
.length()
How do you find out what is at a specific index in a string?
.charAt(int index)
How do you find the index of a specific item in a string?
indexOf(char ch) or indexOf(String str)
How do you find the index of a specific item in a string starting from a specific position?
indexOf(char ch, index int)
What happens when you call indexOf for a value that does not exist?
The value -1 is returned
How would you return the value “123” from the object:
String str = “012345”;
str.substring(1,4) // value at the ending index will not be included
What is the method signature to replace characters?
str.replace(orig, new)
How do you remove trailing and leading whitespace from a string and what is removed?
.trim() removes space, new line (\n), tab (\t), and carriage return (\r)
What does the .append() method on StringBuilder return?
A reference to itself
List all StringBuilder constructor signatures.
new StringBuilder(); // empty object new StringBuilder("foo"); // contains val "foo" new StringBuilder(10); // Empty, but capacity of 10
What are the two StringBuilder methods for adding text to the string in the object?
.append(String str)
.insert(int index, String str)
What are the two StringBuilder methods for removing text from the string in the object?
.delete(int startIndex, int stopIndex) // stopIndex is not removed, same as substring
.deleteChatAt(int index)
Which StringBuilder method is used to reorder the text backwards?
.reverse()
What is StringBuffer?
The same as StringBuilder, but slower because it’s thread safe.
What does the StringBuilder.equals(StringBuilder obj) test?
Checks for reference equality (not value equality)
for
char[] letters;
what kind of variable is letters?
A reference variable pointing to an array of primitive char types
What kind of data structure is an array?
Ordered list
How do you create an empty array of ints?
int[] numbers = new int[3];
How do you create an array of ints with values? (2 ways)
int[] numbers = new int[] {11, 20, 45};
int[] numbers = {11, 20, 45};
What are acceptable variable definitions for an array?
int[] name;
int [] name;
int name[];
int name [];
What type are primitive arrays: primitives or object references?
Object references
How do you get the number of elements in an array?
arr.length;
How do you get a string representation of an array?
java.util.Arrays.toString(myArray);
How do you sort an array?
java.util.Arrays.sort(myArray);
What are the rules for binary search?
- Sorted means “smallest to largest”
- Target element found in a sorted array = index of match
- Target element not found in sorted array = Tells us where to insert to keep the array sorted. Does: neg. desired index minus 1. eg, if the target is index 4, would be -4 - 1 = -5.
- If unsorted (must be smallest to largest), result will be unpredictable.
How does binary search work?
- Divides the data set into two equal parts
- Determines which half the data should exist in
- repeats until data is found
- Only works on sorted data sets (for obvious reasons)
What is the syntax for varargs?
String… args;
List all ways to declare a 2D array
int[][] coords;
int coords[][];
int[] coords[];
How do you define an empty asymmetric multidimensional array?
Only define the first (top level) dimension, then go through each element and define each dimension separately:
int[][] args = new int[2][];
args[0] = new int[5];
args[1] = new int[3];
How do you import ArrayList?
import java.util.ArrayList;