linkedList使用
基本方法回顧 2020-10-18
public class LinkedListDemo {
/**
? ? * 已知數(shù)組存放一批扣扣號(hào) 最長(zhǎng)為11位
? ? * 最短為5位? 數(shù)組定義 合理即可
? ? * 將該數(shù)組里面的 所有扣扣號(hào)都存放在linkedList 當(dāng)中 將list重復(fù)元素刪除
? ? * 將list中所有元素都用迭代器 和增強(qiáng)for循環(huán)打印出來(lái)
? ? * <p>? ? * 有了數(shù)組 就要設(shè)計(jì)到數(shù)組的遍歷 和存放
? ? */
? ? //寫一個(gè)重復(fù)元素刪除的方法
? ? public static LinkedList quchong(LinkedList list) {
LinkedList qu =new LinkedList<>();
IteratorList= list.iterator();
while (List.hasNext()) {
String lt = List.next();
if (!qu.contains(lt)) {
qu.add(lt);
}
}
return qu;
}
public static void main(String[] args) {
//定義數(shù)組
? ? ? ? String[] arr = {"12345","12345","54321","123456","654321","1234567890"};
//創(chuàng)建LinkedList 用于存放數(shù)組的
? ? ? ? LinkedList linkedList =new LinkedList<>();
//遍歷
? ? ? ? for (String str : arr) {
//判斷是否包含
? ? ? ? ? ? //if (!linkedList.contains(args)){
? ? ? ? ? ? //如果不包含 就把他存到linkedList中
? ? ? ? ? ? linkedList.add(str);
//}
? ? ? ? }
//調(diào)用重復(fù)元素的方法
? ? ? ? //之前去重完之后要再把它放在list中 遍歷循環(huán)輸出
? ? ? ? LinkedList quchonglist =quchong(linkedList);
//遍歷linkedList 增強(qiáng)for循環(huán)打印輸出
? ? ? ? for (String qq : quchonglist) {
System.out.println(qq +"linkedList----去重");
}
//使用迭代器遍歷
? ? ? ? Iterator linked = quchonglist.iterator();
while (linked.hasNext()) {
//if (!linkedList.contains(arr)){
? ? ? ? ? ? System.out.println(linked.next());
//}
? ? ? ? }
}
}
hashSet的使用
public class HashSetTest {
/**
? ? * 雙色球的規(guī)則:雙色球每注 投注號(hào)碼由6個(gè)紅色球號(hào)碼和一個(gè)藍(lán)色球號(hào)碼組成
? ? * 紅色球號(hào)碼在1-33中選擇 藍(lán)色球號(hào)碼在1-16中選擇 請(qǐng)隨機(jī)生成一注雙色球號(hào)碼
? ? * 要求同色號(hào)碼不重復(fù)
? ? *
? ? * 分析:隨機(jī)生成 Random
? ? * 紅色球 和藍(lán)色球 應(yīng)該是兩個(gè)集合
? ? * 號(hào)碼不重復(fù) 去重是否包含?
? ? * hashset不能存儲(chǔ)重復(fù)的元素
? ? * 回憶hashset 底層是數(shù)組加鏈表 equals和hashcode
? ? * 不能存儲(chǔ)相同的數(shù)據(jù)? 存儲(chǔ)數(shù)據(jù)時(shí)候是無(wú)序的 存儲(chǔ)的數(shù)據(jù)順序不是按照存入時(shí)的順序存入的 和list不一樣
? ? *
? ? * random.nextInt的使用
? ? * random.nextInt(33) 范圍是0~32 所以要加一 不加也行 ? 就是34?
? ? *
*/
? ? public static void main(String[] args) {
//隨機(jī)產(chǎn)生數(shù)
? ? ? ? Random random =new Random();
//創(chuàng)建hashSet 存儲(chǔ)不同的元素
? ? ? ? HashSet redset =new HashSet<>();
//判斷紅球是否小于6個(gè)
? ? ? ? while (redset.size()<6){
//紅色球
? ? ? ? ? ? int red=random.nextInt(33)+1;
redset.add(red);
}
//藍(lán)色球
? ? ? ? int bull = random.nextInt(17);
// redset.add(bull);
System.out.println("中獎(jiǎng)是:");
System.out.print("紅球");
for (Integer reaBall: redset) {
System.out.println(reaBall+",,, ");
}
System.out.println(bull+"籃球");
}
}
Map接口的特點(diǎn)
鍵值對(duì)
鍵值不能重復(fù)
key value?可以為null
hashMap
package comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* @ClassName: MapDemo
* @Description:
* @Author: Zhangliya
* @CreateDate: 2020/10/22 16:43
* @Version 1.0
* @Copyright: Copyright2018-2020? Inc. All rights reserved.
**/
public class MapDemo {
/**
? ? * Map接口的特點(diǎn)
? ? * 雙列集合
? ? * key value鍵值對(duì)
? ? * key value 都可以為空
? ? * key 不能重復(fù)
? ? * <p>? ? * map接口中常用的方法
? ? * put 添加元素
? ? * get 獲取元素
? ? * remove
? ? * <p>? ? * 往一個(gè)Map中 添加若干的元素 獲取Map中所有的value 并使用增強(qiáng)for和迭代器遍歷輸出每個(gè)value?
*/
? ? public static void main(String[] args) {
//新建Map
//? ? ? ? HashMap hashMap = new HashMap<>();
//? ? ? ? hashMap.put("北京","真好");
//? ? ? ? hashMap.put("南京","也好");
//? ? ? ? hashMap.put("北京1","真好");
//? ? ? ? hashMap.put("南京1","也好");
//? ? ? ? for (Object value: hashMap.values() ) {
//
//? ? ? ? ? ? System.out.println(value);
//? ? ? ? }
//? ? }
/**
? ? ? ? * 使用Map集合存儲(chǔ)自定義數(shù)據(jù)類型car做鍵 對(duì)應(yīng)的價(jià)格做值 并使用keySet和EntrySet兩種方式 遍歷map
*
*
? ? ? ? * 分析:
? ? ? ? * 1.Map集合
? ? ? ? * 2.自定義數(shù)據(jù)類型
? ? ? ? * 3.car 做鍵 key=""car""?
? ? ? ? * 4.遍歷map的兩種方式 不知道 !
? ? ? ? * 5. car小汽車所以需要定義類
? ? ? ? *
? ? ? ? * map.value value值
? ? ? ? *
? ? ? ? * map中keyset 和Entryset遍歷的區(qū)別
? ? ? ? * keyset返回值是個(gè)存放的key值的set集合(集合中無(wú)序存放)
? ? ? ? * keySet獲取map集合的所有鍵的set集合? 有了set集合 就可以使用迭代器
? ? ? ? * entryset 返回映射所包含的映射關(guān)系的集合 (一個(gè)關(guān)系就是一個(gè)鍵值對(duì)) 就是把key value作為一個(gè)整體 一個(gè)一個(gè)的放進(jìn)set集合中
? ? ? ? *
*/
? ? ? ? HashMap hashMap =new HashMap<>();
//添加車到hashMap中
? ? ? ? MapDemocar baoma =new MapDemocar("寶馬","23");
MapDemocar baoma1 =new MapDemocar("寶馬1","255");
MapDemocar baoma2 =new MapDemocar("寶馬","23");
hashMap.put(baoma,20);
hashMap.put(baoma1,2050);
hashMap.put(baoma2,2000);
//keyset 遍歷
? ? ? ? Set carSet = hashMap.keySet();
//tor iteratorCar = carSet.iterator();
// (iteratorCar.hasNext()){
//teratorCar.next();
? ? ? ? for (MapDemocar aa : carSet) {
Integer value = hashMap.get(aa);
System.out.println(value);//20 2050 2000
? ? ? ? }
System.out.println("--------------------------------------------------");
Set> entrySetcar = hashMap.entrySet();
for (Map.Entry entry : entrySetcar
) {
MapDemocar key = entry.getKey();
Integer value1 = entry.getValue();
System.out.println(key +"www" + value1);
}
}
}
