声明一个数组

String[] aArray = new String[5];

String[] bArray = {"a","b","c", "d", "e"};

String[] cArray = new String[]{"a","b","c","d","e"};



打印一个数组

int[] intArray = { 1, 2, 3, 4, 5 };

String intArrayString = Arrays.toString(intArray);

System.out.println(intArray);// [I@7150bd4d

System.out.println(intArrayString);// [1, 2, 3, 4, 5]



根据数组创建ArrayList

String[] stringArray = { "a", "b", "c", "d", "e" };

ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));

System.out.println(arrayList);// [a, b, c, d, e]



判断数组内部是否包含某个值

String[] stringArray = { "a", "b", "c", "d", "e" };

boolean b = Arrays.asList(stringArray).contains("a");

System.out.println(b);// true



连接两个数组

int[] intArray = { 1, 2, 3, 4, 5 };

int[] intArray2 = { 6, 7, 8, 9, 10 };// Apache Commons Lang library

int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2);



声明一个内联数组(array inline)

method(new String[]{"a", "b", "c", "d", "e"});



根据分隔符拼接数组元素(去掉最后一个分隔符)

// containing the provided list of elements// Apache common lang

String j = StringUtils.join(new String[] { "a", "b", "c" }, ", ");

System.out.println(j);// a, b, c



ArrayList转数组

String[] stringArray = { "a", "b", "c", "d", "e" };

ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));

String[] stringArr = new String[arrayList.size()];

arrayList.toArray(stringArr);

for (String s : stringArr)

    System.out.println(s);



Array转Set

Set<String> set = new HashSet<String>(Arrays.asList(stringArray));

System.out.println(set);//[d, e, b, c, a]



反转数组

int[] intArray = { 1, 2, 3, 4, 5 };

ArrayUtils.reverse(intArray);

System.out.println(Arrays.toString(intArray));//[5, 4, 3, 2, 1]



删除数组元素

int[] intArray = { 1, 2, 3, 4, 5 };int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array

System.out.println(Arrays.toString(removed));



整形转字节数组

byte[] bytes = ByteBuffer.allocate(4).putInt(8).array();for (byte t : bytes) {

    System.out.format("0x%x ", t);

}


HashMap用法

实例化:

HashMap<String,String> map= new HashMap<String,String>();


常用方法:

(1)put(K key, V value)

(2)get(Object key)

(3)size()

(4)clear()

(5)isEmpty ()

(6)remove(Object key)

(7)values()


(8)keySet() 

//将key作为元素转存入一个set集合。

Set<String> set = map.keySet();

for (String key : set) {

System.out.println(key + " " + map.get(key));

}

(9)entrySet() 

//将每一组key-value变为一个entry对象存入set集合

Set<Entry<String, Integer>> set = map.entrySet();

for (Entry<String, Integer> entry : set) {

System.out.println(entry.getKey() + ":" + entry.getValue());}


(10)iterator 迭代器

   a、与get()方法结合:

b、与entry对象结合:

Java中一些数据结构的API用法_Apache


栈用法


Stack<Integer> stack = new Stack<>();


常用方法:

push( num) //入栈

pop() //栈顶元素出栈

empty() //判定栈是否为空

peek() //获取栈顶元素

search(num) //判断元素num是否在栈中,如果在返回1,不在返回-1。 

Java中一些数据结构的API用法_Apache_02



队列


//add()和remove()方法在失败的时候会抛出异常(不推荐) 

Queue<Stringqueue new LinkedList<String>();


offer()  //添加元素 

Poll()   //返回第一个元素,并在队列中删除

element()  //返回第一个元素

queue.peek() //返回第一个元素  2. Get the first element - return null if queue is empty.


size()

Java中一些数据结构的API用法_System_03