一.數組和集合注意事項
1。在使用工具類Arrays.asList()把數組轉集合的時候,不能使用其修改的其它方法,他的add/remove/clear方法會拋出異常
原理:其返回的對象是一個Array內部類,並沒有實現集合的修改方法,其只是體現適配器模式,只是轉換接口,後臺依舊是數組
2。不要在foreach循環中進行元素remove/add操作。remove元素使用Iterator方式
3.推薦使用entrySet遍歷Map類集合,而不是keySet方式便利,因爲keyset其實遍歷了兩次
4.使用集合轉成數組的方法,必須使用集合的toArray(T【】 array) ,傳入的是類型完全一樣的數組,,大小就是list.size()
5.
集合類 | key | value | super | 說明 |
Hashable | 不能爲null | 不能爲null | Dictionary | 線程安全 |
ConcurrentHashMap | 不能爲null | 不能爲null | AbstractMap | |
TreeMap | 不能爲null | 可以爲null | AbstractMap | 不安全 |
HashMap | 可以爲null | 可以爲null | AbstractMap | 不安全 |
6.利用set元素唯一性,可以對集合去重,避免使用list集合的contains方法進行遍歷,對比,去重
二.數組
一維數組
1.數組定義
type[] arrayName 或type arrayName []
int [] arr 或是 int arr2 []
2.靜態初始化
type arrayName = new type[]{element1,element2,element3}
例子:int arr []=new int []{1,2,3};
//普通的數組遍歷方式
for(int i=0 ;i<arr.length;i++){
system.out.println(arr[i])
}
// foreach方式
for(int z:arr){
system.out.println( z );
}
數組如何取出來,就是用 arr[i]即可
3.動態初始化
arrayName =new type[length]
int arr2[]=new int[3];
arr[0]=1 ;//給數組元素賦值,
int類型默認的數值爲0;
二維數組
靜態初始化
arrayName =new type[]{{element1,element2},{element1,element2},{element1,element2}}
例子:int[][] arr =new int[][]{{1,2,3},{1,2,3},{1,2,3}}
動態初始化
arrayName=new type[length][length]
例子:int[][] arr =new int[3][3];
arr[1][2]=3//賦值
例子:int arr[][] =new int[][]{{1,2,3},{4,5,6,9},{6,7,8}}
for(int i=0;i<arr.length;i++){
for(int j<0;j<arr[i].length;j++){
system.out.print(arr[i][j] }
}
數組排序例子——————起泡法
對4 ,21,0 ,-12 ,-3排序升序
int arr [ ] ={4,21,-3,-12,0}
int temp;
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr.length-1-i,j++){
if(arr[j]>arr[j+1]){
temp=arr[j];
arr[j+1]=arr[j]
}
}
}
for(int a:arr){
system.out.println(a)
}
三 集合
1.用數組存儲
Student students[] =new Student[3];
student[0] =new Student("張三",10);
student[1] =new Student("李四",10);
student[2] =new Student("王五",10);
2.List<E>集合
List是collection接口的子接口。List集合裏的元素是可以重複的
List接口的主要實現類有Arraylist 和Linkedlist
2.1Arraylist<E>
ArrayList<String> arraylist =new ArrayList<String>();
arraylist.add("張三");
arraylist.add("李四");
//將指定元素插入到列表中的指定位置 例子:將王五插到第二個位置
arraylist.add(1,"王五");
get(int index) :返回此列表中指定位置上的元素
//將指定的元素替代此列表中指定位置元素
arraylist.set(1,"小王五");
2.2 Linkedlist<E>
LinkedList<String > linkedlist= new LinkedList<String > ();
linkedlist.add("張三");
linkedlist.add("李四");
linkedlist.add("李五");
其特有的方法有:
indexof() 返回此列表中首次出現的指定元素的索引,如果此列表中不包含該元素,則返回-1
如 linkedlist.indexof("李四");
peekFirst()獲取但是不移除列表中的第一個元素,如果此列表爲空,則返回NULL
peekLast()獲取但是不移除列表中的最後一個元素,如果此列表爲空,則返回NULL
3.遍歷
LinkedList<Student > list= new LinkedList<Student > ();
list.add(new Student("張三",10));
list.add(new Student("李四",10))
list.add(new Student("王五",10))
使用迭代器遍歷 iterator
Iterator<Student> it=list.iterator()
while(it.hasNext()){
Student s=it.next();
System.out.println(s);
}
使用for遍歷
for( student s :list ){
System.out.println(s);
}
4.set集合是collection接口
Hashset 重要特點:1.不允許存在重複的值 2.無序的
HashSet<String> hs =new HashSet<String> ()
hs.add("232");
hs.add("25");
hs.add("22");
Iterator<String> it =hs.iterator;
while(it.hasNext()){
String s=it.next();
System.out.printIn(s);
}
5. map<k,v>
HashMap<String,Student> hashMap =new Hash<String,Student>();
hashMap .put("1號",new student("張三",10));
hashMap .put("2號",new student("王五",10));
hashMap .put("3號",new student("李四",10));
Student s =hashMap.get("1號");
//獲取Key的集合,再獲取迭代器
Iterator<String> it=hashMap.keySet().iterator()
whlie(it.hasNext()){
String key=it.next(); //獲取key
student student =hashMap.get(key) //獲取值
System.out.printIn(s)
)
6.list map set區別
1.list列表是順序存放的,可以有相同的對象,通過索引存取。
2.set集合是無序存放的,不能有重複的對象,集合無索引,只能通過遍歷存取。
3.map:存放的是鍵和值的映射,其中鍵是唯一的值,可以有重複的對象
三者聯繫和區別:三者都是接口。list和set都是單列元素的集合。list和set都是繼承了collection.而map不是