Java常用類及其常用方法

jopen 9年前發布 | 2K 次閱讀 Java

1、ArrayList


java.util.ArrayList<E>

add(E e)              //插入尾部
add(int index, E element)
remove(int index)
remove(Object o)
get(int index)
indexOf()
lastIndexOf()
isEmpty()
size()
iterator()
listIterator()



java.util 接口 Iterator<E>

hasNext()
next()
remove()


2、Arrays 和Collections工具類


System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length) 


java.util.Arrays

static int binarySearch()      //找不到則返回(-(插入點) - 1),不一定是-1。
static boolean equals()       //判斷數組相等
static void fill()      
static void sort()
static <T> void sort(T[] a, Comparator<? super T> c) 


java.util.Collections


static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key) 
max(Collection<? extends T> coll) 
min(Collection<? extends T> coll) 
static void reverse(List<?> list) 
sort(List<T> list) 
static <T> void sort(List<T> list, Comparator<? super T> c) 
static <T> Comparator<T> reverseOrder()         //重要

java.util.接口Comparator<T>

public interface Comparator<T> {
    int compare(T o1, T o2);
    boolean equals(Object obj);
}


java.lang.接口Comparable<T>

public interface Comparable<T> {
    int compareTo(T o);
}


java.util.BitSet

BitSet()
BitSet(int nbits)        //默認情況下,set中所有位的初始值都是false

void clear(int bitIndex)   //將索引指定處的位設置為 false。 
void clear()               //將此 BitSet 中的所有位設置為 false。 
void flip(int index)        //反轉指定位
boolean get(int index)
BitSet get(int from, int to)
boolean isEmpty()           //當沒有任何true位時,返回true
int length()                //最高位索引+1(最高的1的索引,不是BitSet(10)中的9)
void set(int bitIndex)     //將指定索引處的位設置為 true。 
void set(int fromIndex, int toIndex, boolean value)    //將指定的 fromIndex(包括)到指定的 toIndex(不包括)范圍內的位設置為指定的值。 
String toString()          //返回字符串表示,bs.set(4);bs.set(6);之后為"{4,6}"




3、HashMap


java.util.HashMap<K, V>

boolean containsKey(Object key) 
boolean containsValue(Object value) 
Set<Map.Entry<K,V>> entrySet() 
V get(Object key)
boolean isEmpty()
Set<K> keySet()
V put(K key, V value)       //添加鍵值對,如果存在key,則替換value
V remove(Object key)
int size()




java.util.接口 Map.Entry<K,V>         //是Map接口的靜態內部接口

public static interface Map.Entry<K,V>

K getKey()
V getValue()
V setValue(V value)


4、HashSet


java.util.HashSet<E>


boolean add(E e)
boolean remove(Object o)
boolean contains(Object o)
boolean isEmpty()
int size()
Iterator<E> iterator()


5、LinkedList


java.util.LinkedList<E>


public class LinkedList<E> extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, Serializable



boolean add(E e)                    //加到末尾
void addFirst(E e)
void addLast(E e)
E removeFirst()
E removeLast()

E element()                   //獲取但不移除頭
E get(int index)
E getFirst()
E getLast()

int indexOf(Object o)                 //不包含則返回-1
int lastIndexOf(Object o)             //不包含則返回-1
boolean contains(Object o)

int size()
Iterator<E> iterator()

E set(int index, E element)




6、Object


finalize()
clone()

3個wait()
notify()
notifyAll()

getClass()
equals()
hashCode()
toString()


一共11個方法!!


7、Pattern 和 Matcher

java.util.regex.Pattern


static Pattern compile(String regex)
static boolean matches(String regex, CharSequence input)

Matcher matcher(CharSequence input)




java.util.regex.Matcher


while(m.find()) {
    String str1 = m.group();
    String str2 = m.group(2);
    boolean b1 = m.lookingAt();        //頭部匹配
    boolean b2 = m.matches();
}

int start()    //以前匹配的初始索引
int end()   //以前最后匹配字符之后一個字符的偏移量
Matcher reset()  //重置匹配器
String replaceAll(String replacement)     //替換模式與給定替換字符串相匹配的輸入序列的每個子序列。
String replaceFirst(String replacement)


8、String


static Comparator<String> CASE_INSENSITIVE_ORDER;

charAt()
compareTo()
compareToIgnoreCase()
contains()
startsWith()
endsWith()
equals()             //判斷字符串相等不能用==
indexOf()
lastIndexOf()
isEmpty()
length()
matches(String regex)
replace()
replaceAll(String regex, String replacement)
replaceFirst(String regex, String replacement)
String[] split(String regex)
substring(int begin)                  //注意方法寫法
substring(int begin, int end)          //注意方法寫法, end不包括
toLowerCase()
toUpperCase()
trim()                      //忽略前后的空白



9、StringBuilder


StringBuilder()
StringBuilder(String str)


append()
capacity()   與length()的區別!!建議忘了它吧!!
charAt()
delete(int start, int end)    //不包含end
indexOf()
lastIndexOf()
insert(int offset, CharSequence s)
length()
reverse()
substring(int begin)                  //注意方法寫法
substring(int begin, int end)          //注意方法寫法, end不包括
String toString()

沒有沒有equals()方法!!!!!!!!
沒有沒有equals()方法!!!!!!!!
沒有沒有equals()方法!!!!!!!!


 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!