Collections
List
ArrayList长度不受限制实现:当不够用,则将本数组拷贝到另外一个新数组,新数组的长度为:原数组长度+原数组长度的一半
Collection<String> c = new ArrayList<>();
List<String> c = new ArrayList<>();//两种写法都可以,因为List继承了Collection
System.out.println(c.size()); //集合长度
System.out.println(c.isEmpty()); //集合是否为空
c.add("CHINA"); //集合中添加元素
c.add("Englend");
c.add("d");
// c.remove("Englend"); //删除元素
// c.clear(); //清空所有元素
c.toArray();//转换为数组
c.add(2,"巧碧螺");//在索引为2的位置插入
c.set(2,"巧碧螺");//将索引为2的元素替换
System.out.println(list.indexOf("d"));//从头开始找,元素的索引
System.out.println(c.contains("CHINA")); //是否包含某个对象
Map
键值对进行存储。Map中的键值不能重复,否则会覆盖原数据。
- 简单用法
Map<Integer,String> m1 = new HashMap<Integer, String>();
Map<Integer, String> m2 = new HashMap<Integer, String>();
m2.put(4, "四");
m2.put(5, "五");
m1.put(1,"one"); //插入一个元素
m1.put(2,"two");
m1.put(3,"three");
m1.putAll(m2);//插入一个Map中的所有元素
System.out.println(m1.get(1));//根据键值得到元素值
System.out.println(m1.containsKey(1));//判断是否包含某个键
System.out.println(m1.containsValue("one"));//判断是否包含某个值
- map传递对象示例
public class TestMap2 {
public static void main(String[] args) {
Employee e1 = new Employee(1001, "老鼠1", 60000);
Employee e2 = new Employee(1002, "老鼠2", 50000);
Employee e3 = new Employee(1003, "老鼠3", 40000);
Map<Integer, Employee> map = new HashMap<Integer, Employee>();
map.put(1001, e1);
map.put(1002, e2);
map.put(1003, e3);
Employee employee = map.get(1002);
System.out.println(employee.getEname());
System.out.println(map);
}
}
class Employee {
private int id;
private String ename;
private double salary;
public Employee(int id,String ename,double salary) {
super();
this.id = id;
this.ename = ename;
this.salary = salary;
}
public int getId() {
return id;
}
public String getEname() {
return ename;
}
public double getSalary() {
return salary;
}
}
set
区别为:没有顺序,不可重复。
Set<String> set1 = new HashSet<String>();
set1.add("aa");
set1.add("bb");
set1.add("aa"); //输出只有aa bb
- list/set遍历
List<String> list = new ArrayList<String>();
list.add("aa");
list.add("bb");
list.add("cc");
//使用了Iterator迭代器
for(Iterator<String> iter = list.iterator();iter.hasNext();) {
String tempString = iter.next();
System.out.println(tempString);
}
//或者
for(int i=0;i<list.size();i++) {
list.get(i);
}
map遍历
Map<Integer,Man> maps = new HashMap<Integer,Man>();
Set<Integer> keyset = maps.keySet();
for(Integer id:keySet){
System.out.println(maps.get(id).name);
}
- Collections工具类
1.Collections.shuffle(list); 随机排序
2.Collections.sort(list); 按照递增排序
3.Collections.reverse(list); 逆序排列
4.Collections.binarySearch(list,'name'); 二分查找
IO
- 路径建议
pathString = "D:/EclipseWork/07-AOP";
- 构建File对象
String pathString = "C:/Users/002/Desktop/mcd信息平台/leader页面图片/leader首页.png";
//1.构建File对象
File srcFile = new File(pathString);
System.out.println(srcFile.length());
//2.构建File对象
File srcFile1 = new File("C:/Users/002/Desktop/mcd信息平台/leader页面图片","leader首页.png");
System.out.println(srcFile1.length());
- 路径
//相对路径,通常文件存储在项目中
srcFile1 = new File("leader首页.png");
System.out.println("名称:"+src.getName());
System.out.println("路径:"+src.getPath());
System.out.println("绝对路径:"+src.getAbsolutePath());
System.out.println("父路径:"+src.getPath());
System.out.println("是否存在:"+src.exists());
System.out.println("是否文件:"+src.isFile());
System.out.println("是否文件夹:"+src.isDirectory());
- 创建文件
File src = new File("C:/Users/002/Desktop/AA.text");//后缀则为文件类型
src.createNewFile();//创建文件