Está en la página 1de 6

Java 6 Quick

Reference
Especially useful for programming
competitions
String (java.lang.String)
Constructors
String()
Initializes an empty string.
String(String original)
Copies a pre-existing String.
String(char[] value)
Creates a String from a character array.
Memer !unctions
char charAt(int inex)
!eturns the character value at the specifie inex.
int compare"o(String another)
Compares t#o strings lexicographically. An ignore-case variant is also
availa$le.
String concat(String str)
Concatenates parameter % to the en of this string.
$oolean ens&ith(String suffix)
"ests if the string ens #ith a certain suffix.
$oolean e'uals(String str)
"ests if t#o strings are e'ual. An ignore-case variant is also availa$le. (o
"#$ use the e'uality operator.
int inex)f(char ch[* int fromInex])
!eturns the inex of the character #ithin this string. If fromInex is
specifie* it $egins searching at that inex.
int inex)f(String str[* int fromInex])
!eturns the inex of the specifie string #ithin this string. If fromInex is
specifie* it $egins searching at that inex.
int lastInex)f(char ch)
!eturns the last inex of the specifie character (or string* if one is
specifie).
int length()
!eturns the string+s length.
String replace(char o* char n)
!eplaces all occurrences of the character o #ith n.
$oolean starts&ith(String prefix)
"ests if the string starts #ith the specifie prefix.
String su$string(int $eginInex[* int enInex])
!eturns a string containing the characters $et#een $eginInex an
enInex* not incluing enInex. If enInex is not specifie* it is
assume to represent the en of the string.
String to,o#erCase() - to.pperCase()
Converts the string to a certain case.
%rray&ist (java.util.%rray&ist)
Constructors
Array,ist/01()
Initializes an empty Array,ist of o$2ect 0.
Array,ist/01 (Collection/3 extens 01 c)
Initializes an Array,ist of type 0 that contains the given collection.
Array,ist/01(int initialCapacity)
Initializes an empty Array,ist #ith memory allocate for initialCapacity
elements.
Memer !unctions
voi a(0 e)
As e to the en of the list.
voi a(int inex* 0 e)
As e to the list at the specifie inex.
voi remove(int inex)
removes the element at the specifie inex form the list
voi clear()
!emoves all elements from the list
voi get(int inex)
gets the element at the specifie inex
voi set(int inex* 0 element)
!eplaces the element at the specifie position in this list #ith the specifie
element
$oolean contains()$2ect o)
!eturns true if the o$2ect o is in the list.
int inex)f()$2ect o)
!eturns the inex of the o$2ect o if it is in the list. )ther#ise it returns -%
int lastInex)f(String str)
"ests if t#o strings are e'ual. An ignore-case variant is also availa$le. (o
"#$ use the e'uality operator.
)$2ect clone()
!emoves all of the elements from this list
)$2ect[] toArray()
!eturns an array containing all of the elements in this list in proper
se'uence (from first to last element).
voi remove!ange(int fromInex* int toInex)
!emoves from this list all of the elements #hose inex is $et#een
fromInex* inclusive* an toInex* exclusive.
%rrays (java.util.%rrays)
Memer !unctions
'(at is meant y an array is an array of any type (primitives or ojects
)(ic( implement comparator)
static int $inarySearch(an array) 4 array (as to e sorte* to use t(is function
Searches the specifie array for the specifie value using the $inary search
algorithm.
static an array copy)f(anarray* int ne#,ength)
Copies the specifie array* truncating or paing #ith nulls or zeros
epening on the type (if necessary) so the copy has the specifie length
static an array copy)f!ange(an array* int ne#,ength)
Copies the specifie range of the specifie array into a ne# array.
static String eep"oString()$2ect[] a)
!eturns a string representation of the 5eep contents5 of the specifie array
or in ot(er )or*s returns the output of the toString() function of each
element.
static $oolean e'uals(an array* another array)
!eturns true if the t#o specifie arrays are e'ual to one another.
static voi sort(an array)
sorts the array in ascening orer
static String toString(an array)
!eturns a string representation of the contents of the specifie array.
$reeMap+,-./ (java.util.$reeMap)
Rememer t(at Maps are al)ays sorte* y t(e key
Constructors
"ree6ap/7*81()
Initializes an empty "ree6ap.
Memer !unctions
voi put(7 9ey* 8 value)
Associates the specifie value #ith the specifie 9ey in this map.
8 get()$2ect 9ey)
!eturns the value to #hich the specifie 9ey is mappe* or null if this map
contains no mapping for the 9ey.
8 remove()$2ect 9ey)
!emoves the mapping for this 9ey from this "ree6ap if present.
int size()
!eturns the num$er of 9ey-value mappings in this map.
voi clear()
!emoves all of the mappings from this map.
voi clone()
!eturns a shallo# copy of this "ree6ap instance.
Stack (java.util.Stack)
Rememer (o) stacks )ork0 !ollo) !1&#(first in last out). 1tems are
al)ays pus(e*(a**e*) onto t(e top of t(e stack.
Constructors
Stac9/01()
Initializes an empty Stac9 of o$2ect 0.
Memer !unctions
$oolean empty()
"ests if this stac9 is empty.
0 pee9()
!eturns the o$2ect at the top of this stac9 #ithout removing it from the
stac9.
0 pop()
!emoves the o$2ect at the top of this stac9 an returns it.
0 push(0 item)
:ushes an item onto the top of this stac9.
int search()$2ect o)
!eturns the %-$ase position #here an o$2ect is on this stac9.
%-;ase position means that the function starts counting the inex from %
instea of <. Also* the function consiers the topmost item the first one
=ere is a function in 2ava to fin the least common su$string $et#een t#o strings>
public static int lcs_len(String str1, String str2) {
int[][] d = new int[str1.length() + 1][str2.length() + 2];
!r (int i = "; i #= str1.length(); i++) d[i]["] = ";
!r (int i = "; i #= str2.length(); i++) d["][i] = ";
!r (int i = 1; i #= str1.length(); i++) {
!r (int $ = 1; $ #= str2.length(); $++) {
i (str1.char%t(i&1) == str2.char%t(i&1)) d[i][$] = d[i&1][$&1]+1;
else d[i][$] = 'a((d[i][$&1], d[i&1][$]); )) where 'a( inds the 'a(i'u' !
tw! integers.
*
*
return d[str1.length()][str2.length()];
*