1,计算格林尼治时间到指定时间的毫秒值,用来验证是否在指定时间内
SimpleDateFormat s=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); String start = "2020年11月11日 00:00:00";//设定开始时间 String end = "2020年11月11日 00:10:00";//设定结束时间 long startTime=s.parse(start).getTime();//将开始时间转换为毫秒值 long endTime=s.parse(end).getTime();//将结束时间转换为毫秒值
接下来可以用if函数判断你是否在该时间内,例如秒杀,签到等
2,遍历,删除的时候,不管用while循环还是增强for循环都需要一个String变量把数组的数据接着
while (it.hasNext()){ String s=it.next(); if (s.length()==2){ it.remove(); } }
这里用String s接着
for (String str : c) { System.out.println(str);
这里用String str接着
3,Collection没有get方法。,想用fori循环get不到index
4,contain()方法,输入String类型返回boolean类型,判断字符串内是否有你输入的内容
5,在加强for循环里比大小
double max=0; for (double s : c) { if (s>max){ max=s; } }
6,从集合中筛选集合的时候,若是调用了带返回值的方法,应该创建两个集合,一个集合是原集合,另一个集合是筛选出来的集合,创建一个筛选方法,把返回值用第二个集合接着,代码如下
public static void main(String[] args) { Student student = new Student(); List<Student> list = new ArrayList<>(); list.add(new Student(22, "张三")); list.add(new Student(26, "李四")); list.add(new Student(38, "张翠山")); list.add(new Student(19, "赵六")); list.add(new Student(103, "张三丰")); list.add(new Student(17, "张无忌")); list.add(new Student(16, "赵敏")); ArrayList<Student> list1 = method1(list); System.out.println(list1); } private static ArrayList<Student> method1(List<Student> list) { ArrayList<Student> list1 = new ArrayList<>(); Iterator<Student> it = list.iterator(); while (it.hasNext()) { Student s=it.next(); if (s.getAge() > 18 && s.getName().contains("张")) { list1.add(s); } } return list1;
结果是筛选出姓张并且大于18岁的人
[Student{age=22, name='张三'}, Student{age=38, name='张翠山'}, Student{age=103, name='张三丰'}]