day19 集合

# # 列表的创建2种方法
# l1 = [1,2,3]
# b = list([4,5,6])
# # 元组的创建2种方法
# t1 = (1,2,3)
# t2 = tuple([4,5,6])
# # 集合的创建有2种方法:不可重复,无序,元素不可变(不能是字典、列表),可以是数字、字符串、元组
# # 创建空集合只能使用set()方法,用大括号会认为创建一个空字典
# # 可哈希和不可哈希
# s = set('alex li')
# ss = {"a","b","c",1,(2,1,3)}
# sss = set()
# print("ss:",ss)
# print("sss",sss)
# print(s)
# s1 = ["alvin","ee","alvin"]
# s2 = set(s1)
# print(s2)
# # 遍历集合
# print("alvin" in s2)
# # 集合添加元素,当作一个元素添加
# s2.add("uu")
# # s2.add([88,99]) 元素不能是列表、字典
# s2.add((44,55))
# print("s2:",s2)
# # 更新,添加新内容,当作一个序列添加,元素可以是列表、字典、数字、字符串、元组
# s2.update("jl")
# print(s2)
# s2.update(["ee",1,5])
# print(s2)
# # 删除元素
# # s2.remove("alvin")    删除指定元素
# print(s2)
# # s2.pop()   删除第一个元素
# print(s2)
# s2.clear()  清空
# print("s2",s2)
# del s2
# print(s2)

a = set([1,2,3,4,5])
b = set([4,5,6,7,8])
# 交集
print(a.intersection(b))
print(a&b)
# 并集
print(a.union(b))
print(a | b)
# 差集
print(a.difference(b))
print(a-b)
print(b.difference(a))
print(b-a)
#并集-交集
print(a.symmetric_difference(b))
print(a ^ b)
# 父集
print(a.issuperset(b))
print(a > b)
# 子集
print(a.issubset(b))
print(a < b)

  

转载于:https://www.cnblogs.com/xiaojiaoma/p/11212447.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是Java代码实现您的需求: ```java import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class CreateFiles { public static void main(String[] args) { // 创建task02目录 String dirPath = "./day19/task02"; File dir = new File(dirPath); if (!dir.exists()) { dir.mkdirs(); } // 使用集合List去存储数据 List<Map<String, String>> fileList = new ArrayList<>(); Map<String, String> fileMap1 = new HashMap<>(); fileMap1.put("name", "task02"); fileMap1.put("type", "dir"); fileList.add(fileMap1); Map<String, String> fileMap2 = new HashMap<>(); fileMap2.put("name", "test01"); fileMap2.put("type", "dir"); fileList.add(fileMap2); Map<String, String> fileMap3 = new HashMap<>(); fileMap3.put("name", "File类操作.avi"); fileMap3.put("type", "file"); fileList.add(fileMap3); Map<String, String> fileMap4 = new HashMap<>(); fileMap4.put("name", "File 学习笔记.md"); fileMap4.put("type", "file"); fileList.add(fileMap4); Map<String, String> fileMap5 = new HashMap<>(); fileMap5.put("name", "Test02"); fileMap5.put("type", "dir"); fileList.add(fileMap5); Map<String, String> fileMap6 = new HashMap<>(); fileMap6.put("name", "java学院-讲师上云-日期讲解.avi"); fileMap6.put("type", "file"); fileList.add(fileMap6); Map<String, String> fileMap7 = new HashMap<>(); fileMap7.put("name", "java学院-讲师上云-日期解析.avi"); fileMap7.put("type", "file"); fileList.add(fileMap7); Map<String, String> fileMap8 = new HashMap<>(); fileMap8.put("name", "任务作业.txt"); fileMap8.put("type", "file"); fileList.add(fileMap8); // 遍历集合, 创建文件及其文件夹 for (Map<String, String> file : fileList) { String fileName = file.get("name"); String fileType = file.get("type"); if ("dir".equals(fileType)) { File newDir = new File(dirPath + "/" + fileName); if (!newDir.exists()) { newDir.mkdirs(); } } else if ("file".equals(fileType)) { File newFile = new File(dirPath + "/" + fileName); try { newFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } } System.out.println("文件及其文件夹创建成功!"); } } ``` 运行这段代码后,程序会在day19目录下创建一个名为task02的文件夹,并在其中创建对应的文件及其文件夹。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值