《移动平台开发实践》第4周作业
上课日期:2019年3月20日
一、知识点总结
课本笔记
第十二章
enum 用法一:enum常量public enum Color{RED, GREEN, BLANK, YELLOW}
用法二:类成员
package practice;public class Shape {private enum ShapeType{RECT,TRIA,OVAL;};private ShapeType type=ShapeType.RECT;public String toString(){if(this.type==ShapeType.RECT){return "this is a RECT";}if(this.type==ShapeType.OVAL){return "this is a RECT";}if(this.type==ShapeType.TRIA){return "this is a TRIA";}return "shape is tria";}}
用法三:遍历
for(CustomerType customerType : CustomerType.values()){System.out.println("Customertype");}//INDIVIDUAL//ORGANIZATON
switch的case语句中也可使用
case INDIVIDUAL:System.out.println("Type is INDIVIDUAL.");break;
十三章
Instant:Java新纪元时间点的引用(1970-01-01-00:00)package practice;import java.time.Duration;import java.time.Instant;public class InstantDemo1{public static void main(String[] args){Instant start=Instant.now();System.out.println("Hello World");Instant end=Instant.now();System.out.println(Duration.between(start,end).toMillis());}}
LocalDate:没有日期,没有时区.
package practice;import java.time.LocalDate;import java.time.temporal.ChronoField;import java.time.temporal.ChronoUnit;public class LocalDate{public static void main(String[] args){LocalDate today=LocalDate.now();LocalDate tomorrow=today.plusDays(1);LocalDate oneDecadeAgo=today.minus(1,ChronoUnit.DECADES);System.out.println("Day of month:"+today.getDayOfMonth());System.out.println("Today is"+today);System.out.println("Tomorrow is"+tomorrow);System.out.println("A decade ago was"+oneDecadeAgo);System.out.println("Year:"+today.get(ChronoField.YEAR));System.out.println("Day of year:"+today.getDayOfYear());}}
package practice;import java.util.Date;/*** Created by IntelliJ IDEA.* User: leizhimin* Date: 2007-11-30* Time: 8:45:44* 日期测试*/public class TestDate {public static void main(String args[]) {TestDate nowDate = new TestDate();nowDate.getSystemCurrentTime();nowDate.getCurrentDate();}/*** 获取系统当前时间* System.currentTimeMillis()返回系统当前时间,结果为1970年1月1日0时0分0秒开始,到程序执行取得系统时间为止所经过的毫秒数* 1秒=1000毫秒*/public void getSystemCurrentTime() {System.out.println("----获取系统当前时间----");System.out.println("系统当前时间 = " + System.currentTimeMillis());}/*** 通过Date类获取当前日期和当前时间* date.toString()把日期转换为dow mon dd hh:mm:ss zzz yyyy*/public void getCurrentDate() {System.out.println("----获取系统当前日期----");//创建并初始化一个日期(初始值为当前日期)Date date = new Date();System.out.println("现在的日期是 = " + date.toString());System.out.println("自1970年1月1日0时0分0秒开始至今所经历的毫秒数 = " + date.getTime());}}
Period:基于日期的一个时间量。
LocalDateTime:没有时区的日期时间。 时区package practice;import java.time.ZoneId;import java.util.ArrayList;import java.util.Collections;import java.util.List;import java.util.Set;public class TimeZoneDemo1{public static void main(String[] args){SetallZoneIds=ZoneId.getAvailableZoneIds();List zoneList=new ArrayList<>(allZoneIds);Collections.sort(zoneList);for(String zoneId:zoneList){System.out.println(zoneId);}}}
ZonedDateTime:有时区时间.
Duration:于时间的时间段.package practice;public class DurationDemo1 {import java.time.Duration;import java.time.LocalDateTime;public class DurationDemo1{public static void main(String[] args){LocalDateTime dateTimeA=LocalDateTime.of(2015,1,26,8,10,0,0);LocalDateTime dateTimeB=LocalDateTime.of(2015,1,26,11,40,0,0);Duration duration=Duration.between(dateTimeA,dateTimeB);System.out.printf("There are %d hours and %d minutes.%n",duration.toHours(),duration.toMinutes()%60);}}}
第十四章
数组缺乏灵活性,集合框架可以更好的比较对象和排序。
集合框架的主要类型是Collection接口。package practice;public class IteratorExample {public static void main(String[] args) {ArrayLista = new ArrayList ();a.add("aaa");a.add("bbb");a.add("ccc");System.out.println("Before iterate : " + a);Iterator it = a.iterator();while (it.hasNext()) {String t = it.next();if ("bbb".equals(t)) {it.remove();}}System.out.println("After iterate : " + a);}}/*for(CustomerType customerType : CustomerType.values()){System.out.println("Customertype");}//INDIVIDUAL//ORGANIZATON*/
二、学习中遇到的问题及解决
- 问题1:list set map易混淆
- 问题1解决方案:绘制表格对比
允许重复? | |
---|---|
List | √ |
Set | × |
Map | × |