java常用方法
Posted 石头-Stone
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java常用方法相关的知识,希望对你有一定的参考价值。
1、
byte[] b = {1,2,3,4,5,6,7};
byte[] c = new byte[5];
void System.arraycopy(b,2,c,0,c.length);
//后5个
2、
new int[1].length;
new String("1").length();
3、Object
java.lang.Object :11
protected Object clone()throws CloneNotSupportedException
public boolean equals(Object obj)
protected void finalize()throws Throwable
public final Class<?> getClass()
public int hashCode()
public String toString()
public final void notify()
public final void notifyAll()
public final void wait()throws InterruptedException
public final void wait(long timeout)throws InterruptedException
public final void wait(long timeout,int nanos)throws InterruptedException
:public interface Cloneable--clone()
4、String
public final class String extends Object implements Serializable, Comparable<String>, CharSequence
String(char[] value)
String(char[] value, int offset, int count)//offset开始位置
String(StringBuffer buffer)
public int length()
public boolean isEmpty()
public char charAt(int index)
public void getChars(int srcBegin,int srcEnd,char[] dst,int dstBegin)
public char[] toCharArray()
public static String copyValueOf(char[] data,int offset,int count)
public int compareTo(String anotherString)
public boolean equals(Object anObject)
public boolean equalsIgnoreCase(String anotherString)
public boolean startsWith(String prefix)
public boolean startsWith(String prefix,int toffset)
public boolean endsWith(String suffix)
public int indexOf(int ch)
public int indexOf(int ch,int fromIndex)
public int lastIndexOf(int ch)
public int indexOf(String str)
public int indexOf(String str,int fromIndex)
public int lastIndexOf(String str)
public String substring(int beginIndex)
public String substring(int beginIndex,int endIndex)
public String replace(char oldChar,char newChar)//替换所有字符
public String replaceAll(String regex,String replacement)// replaceAll("\\\\","");
public String replaceFirst(String regex,String replacement)
public boolean matches(String regex)
public String[] split(String regex)
public String[] split(String regex,int limit)
public String toLowerCase()
public String toUpperCase()
public String trim()
public String toString()
:public interface Comparable<T>--int compareTo(T o)
5、LinkedList
public class LinkedList<E>extends AbstractSequentialList<E>implements List<E>, Deque<E>, Cloneable, Serializable
LinkedList(Collection<? extends E> c)
:添加元素
boolean add(E e)
void add(int index, E element)
boolean addAll(Collection<? extends E> c)
boolean addAll(int index, Collection<? extends E> c)
void addFirst(E e)
void addLast(E e)
boolean offer(E e) //元素添加到末尾
boolean offerFirst(E e)
boolean offerLast(E e)
:获取元素
E element() //获取第一个元素
E get(int index)
E getFirst()
E getLast()
E peek() //获取第一个元素
E peekFirst()
E peekLast()
:移除并获取元素
E poll() //第一个元素
E pollFirst()
E pollLast()
E remove() //第一个元素
E remove(int index)
boolean remove(Object o)
E removeFirst()
E removeLast()
int indexOf(Object o)
int lastIndexOf(Object o)
E set(int index, E element) //修改值
boolean contains(Object o)
void clear()
int size()
Object[] toArray()
void push(E e) //堆栈压入元素
E pop() //堆栈弹出元素
栈:
public class Stack<T> {
private LinkedList<T> storage = new LinkedList<T>();
public void push(T v){
storage.addLast(v);
}
public T peek(){
return storage.getLast();
}
public T pop(){
if(!storage.isEmpty())
{
return storage.removeLast();
}
}
public boolean empty(){
return storage.isEmpty();
}
队列:
InitQueue() ——初始化队列
EnQueue() ——进队列
DeQueue() ——出队列
IsQueueEmpty()——判断队列是否为空
IsQueueFull() ——判断队列是否已满
public class Queue
{
private LinkedList storage = new LinkedList();
public void clear()//销毁队列
{
storage.clear();
}
public boolean Empty()//判断队列是否为空
{
return storage.isEmpty();
}
public int Length()//获取队列长度
{
return storage.size();
}
public void enQueue(Object o)//进队
{
storage.addFirst(o);
}
public Object deQueue()//出队
{
if(!storage.isEmpty())
{
return storage.removeLast();
}
return "队列为空";
}
public Object peekQueue()//查看队首元素
{
return storage.getLast();
}
}
6、Map
public class HashMap<K,V>extends AbstractMap<K,V>implements Map<K,V>, Cloneable, Serializable
HashMap(int initialCapacity)
HashMap(Map<? extends K,? extends V> m)
//增删
V put(K key, V value)
void putAll(Map<? extends K,? extends V> m)
V remove(Object key)
//查询
boolean containsKey(Object key)
boolean containsValue(Object value)
V get(Object key)
Set<K> keySet()
Set<Map.Entry<K,V>> entrySet()
//其他
void clear()
boolean isEmpty()
int size()
Object clone()
7、HashSet
public class HashSet<E>extends AbstractSet<E>implements Set<E>, Cloneable, Serializable
HashSet(int initialCapacity)
HashSet(Collection<? extends E> c)
boolean add(E e)
boolean remove(Object o)
boolean contains(Object o)
Iterator<E> iterator()
void clear()
boolean isEmpty()
int size()
Object clone()
7、Collections
public class Collectionsextends Object
static <T> void copy(List<? super T> dest, List<? extends T> src)
static boolean disjoint(Collection<?> c1, Collection<?> c2) //两列表中有相同元素则返回true
static <T> boolean replaceAll(List<T> list, T oldVal, T newVal)
static <T extends Comparable<? super T>> void sort(List<T> list)
static <T> void sort(List<T> list, Comparator<? super T> c)
public interface Comparator<T>{
int compare(T o1, T o2);
boolean equals(Object obj) ;
}
8、Arrays
public class Arraysextends Object
static <T> T[] copyOf(T[] original, int newLength)//复制一定长度
static <T> T[] copyOfRange(T[] original, int from, int to)//复制一定范围
static boolean equals(Object[] a, Object[] a2)
public static boolean deepEquals(Object[] a1,Object[] a2)
static void sort(Object[] a)
static void sort(Object[] a, int fromIndex, int toIndex)
static <T> void sort(T[] a, Comparator<? super T> c)
以上是关于java常用方法的主要内容,如果未能解决你的问题,请参考以下文章
java中常用的包类以及包中常用的类方法属性---util包