JAVA基礎—07.數組&集合

愛轉圈筆記

勤思、體悟、總結、分享

數組&集合

數組(Array)

什麼是數組

數組是相同數據類型(任意類型)的有序集合。

數組也是對象,數組成員相當於對象的成員變量。


數組下標

對數組元素進行編號,從0開始,數組中的每個元素都可以通過下標來訪問。

數組長度是確定的,不可變的。如果下標越界則會拋出異常。


public class TestArray {
	public static void main(String[] args){
		//數組聲明後長度不能改變
		
		//聲明數組的方式:
		//1.長度爲5的整數型空數組(推薦)
		int[] a1 = new int[5];
		//2.長度爲5的整數型空數組
		int a2[] = new int[5];
		//3.長度爲5的固定值數組
		int[] a3 = {1,2,5,4,3};
		
		//獲取數組的長度
		int len = a1.length;
		
		//將數組a3轉換爲字符串
		String str = a3.toString();
		
		//爲數組a4的第1個元素賦值
		a1[3] = 1;
		
		
		//數組工具類Arrays
		//比較數組a1和a2是否相等(有相同的元素)
		boolean b1 = Arrays.equals(a1, a2);
		
		//將數組a2擴容至長度10,此a2已不再是之前的數組,引用發生了改變
		a2 = Arrays.copyOf(a2, 10);
		
		//對數組a3進行升序排列(從小到大)
		Arrays.sort(a3);
		
		//查詢元素val在數組中的下標(要求數組中元素已按照升序排列)。
		int index = Arrays.binarySearch(a1,1);
	}
}

集合/容器(Collection)

什麼是集合

集合可以存放不同類型,不限數量的數據對象

集合存放的是數據對象的引用而不是數據對象的本身


集合/容器的分類

  • Collection(接口,存儲一組不唯一,無序的對象)
    • List(接口,存儲一組不唯一、有序(插入順序)的對象)
      • ArrayList:實現了長度可變的數組,在內存中分配連續的空間。遍歷元素和隨機訪問元素的效率比較高
      • LinkedList:採用鏈表存儲方式。插入、修改、刪除元素時效率比較高
    • Set(接口存儲一組唯一,無序的對象)
      • HashSet:通過哈希值確定元素的位置
    • Map(鍵值對【Key–Value】,Key是以Set方式進行存儲的)
      • HashMap:非線程安全,可以接受null值
      • HashTable:線程安全,不可接受null值

如何遍歷集合?

使用iterator(迭代器)或者for循環


import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class TestCollection {
	public static void main(String[] args){
		
		//聲明一個ArrayList集合
		//查詢速度快
		List<String> arrayList = new ArrayList<String>();
		//向集合中添加元素
		arrayList.add("hello");
		//根據index獲取集合中的元素
		String str = arrayList.get(0);
		//判斷集合中是否包含某元素
		boolean b = arrayList.contains("hello");
		//查找元素在集合中的位置
		int index = arrayList.indexOf("hello");
		//根據index移除集合中的元素
		arrayList.remove(0);
		//移除集合中的指定元素
		arrayList.remove("hello");
		//清空集合,移除集合中的所有元素
		arrayList.clear();
		//集合的大小
		int listSize = arrayList.size();
		
		//遍歷集合方式一,獲取集合的迭代器(推薦使用)
		Iterator<String> ita = arrayList.iterator();
		while(ita.hasNext()){
			//獲取迭代器中的元素
			String s1 = ita.next();
			System.out.println(s1);
		}
		//遍歷集合方式二,for循環
		for (String s2 : arrayList) {
			System.out.println(s2);
		}
		
		
		//聲明一個LinkedList集合
		//增刪改速度快,使用方法同arrayList
		List<String> linkedList = new LinkedList<String>();
		
		/*------------------------------------------*/		
		
		//聲明一個HashSet集合
		Set<String> hashSet = new HashSet<String>();
		//添加元素
		hashSet.add("world");
		//判斷元素是否存在
		boolean exist = hashSet.contains("world");
		//移除元素
		hashSet.remove("world");
		//集合的大小
		int setSize = hashSet.size();
		//遍歷set集合
		Iterator<String> itaSet = hashSet.iterator();
		while(itaSet.hasNext()){
			//獲取迭代器中的元素
			String set = itaSet.next();
			System.out.println(set);
		}
		
		/*------------------------------------------*/		
		
		//聲明一個HashMap集合(常用)
		Map<String, Object> hashMap = new HashMap<String, Object>();
		//添加元素(鍵,值)
		hashMap.put("m1", 1);
		//根據鍵來移除元素(鍵值對)
		hashMap.remove("m1");
		//判斷是否存在指定鍵m1
		boolean bmk = hashMap.containsKey("m1");
		//判斷是否存在指定值1
		boolean bmv = hashMap.containsValue(1);
		//獲取集合中所有鍵
		Set keys = hashMap.keySet();
		//清空集合
		hashMap.clear();
		//集合的大小
		int mapSize = hashMap.size();
		
		
		//聲明一個HashTable集合
		//使用方法同HashMap
		Map<String, Object> hashTable = new Hashtable<String, Object>();
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章