String/Builder/Array/List methods Flashcards
charAt
(int)
returns char at index int.
String/StringBuilder.
String.indexOf
(int(char) | String, ~ | int)
returns first matching index of char or string, -1 otherwise. Second int can be used to set starting point of search.
substring
(int, ~ | int )
returns substring from index first int to either the end of the string or point second int, exclusive.
String/StringBuilder.
trim
()
returns string with surrounding white space (new lines, spaces, tabs) removed.
String.
String.replace
(char, char)|(string, string)
returns string with second char or string wherever first char or string is found.
length
()
returns the natural length of the string.
String/StringBuilder.
startsWith
(String, ~ | int)
returns boolean telling whether or not the string begins with the first string either at the beginning or at the index second int.
String.
endsWith
(String)
returns boolean telling whether or not the string ends with the specified string, starting from the bottom now we’re here.
String.
String Operators
+, +=, ==, !=. + may add numbers before strings.
StringBuilder Constructors
StringBuilder() - creates empty string builder with capacity of 16.
StringBuilder(int) - creates empty string builder with capacity of int.
StringBuilder(String) - created string builder with capacity of string + 16.
append
(*)
returns and mutates StringBuilder with * attached at the end. * can literally be anything. If it is an object whose class has not overridden toString(), classname@hashtag string will be used.
StringBuilder.
insert
(int, *) | (int, String | StringBuilder | char[], int, int)
returns and mutates StringBuilder with * (see append) inserted at first int index position. two ints can be added to specify a substring of the string-like second argument from index second int to the third int, exclusive if String or StringBuilder. if char[], the ints stand for starting position and length of substring.
StringBuilder.
reverse
()
returns and mutates string builder to its reverse.
StringBuilder.
StringBuilder.replace
(int, int, String)
mutates and returns StringBuilder: replaces index positions int to int exclusive with the string. There’s no problem if the second int is over the index; actually, it starts to knock off some of the StringBuilder’s characters without adding white white space.
subSequence
(int, int)
returns a string object wrapped around CharSequence reference from first index int to second, exclusive. does not mutate the string builder.
StringBuilder.
Array Initializations
Object[] aThing = {reference, reference};
Object[] aThing2= new Object[] {reference};
Object[] aThing3;
aThing3 = new Object[] {reference, reference};
Object[] aThing4= new Object[0];
aThing4[0] = reference;
Array.length
array int length accessor.
clone
()
returns a shallow cop: array of the same type with references to the same objects inside. ArrayList clone returns Object reference.
Array, ArrayList.
ArrayList Creation
import java.util.ArrayList; or util.*; ArrayList<type> aThing = new ArrayList<type>(); ArrayList<type> aThing2 = new ArrayList<>(); ArrayList<type> aThing3 = new ArrayList(); ArrayList aThing4 = new ArrayList<type>(); ArrayList aThing5 = new ArrayList(); you can place an argument int to specify capacity. default 10.
add
(rightReference) | (int index, object) add an item to the list and return true. or adds item to specified location and returns nothing. ArrayList
addAll
(rightArrayList) | (int index, rightArrayList)
These methods add all the references from one array list to the other, at a specified int index.
They return true.
Arraylist is really any collections but for the purposes of test 1, it’s array.
The argument array list type can be something that extends the type of the array list.
ArrayList.
remove
(int) | (reference)
removes object at specified int index. returns the removed object. or removes first occurrence of object. returns true if accomplished, false otherwise. uses .equals()
ArrayList.
contains
(reference)
returns true if reference is in there or false otherwise. uses .equals().
ArrayList.
ArrayList.indexOf
(reference)
returns the index of the first occurrence of the element or -1. uses .equals().