Java第十五天(包装类、日期类、日期的格式化 SimpleDateFormat类,集合)

64.包装类java

(1)概念数组

把基本数据类型转换成类,咱们使用基本数据类型作进制转换很麻烦,对于临界值也很差判断,咱们的包装类提供了不少方法供咱们使用,这样会方便不少。缓存

(2)Integer类jvm

public class IntegerTest {
    //int------>Integer
    Integer i=new Integer(10);
    //Integer------>int
    int i1=i.intValue();

    //String----->Integer,Integer构造器中参数必须是数值
    Integer i2=new Integer("55");
    //Integer------>String
    String s=i2.toString();

    //int----->String
    String s1=i1+"";
    String s2=String.valueOf(i1);
    //String------>int
    int i3=Integer.parseInt(s1);
}
public class IntegerTest1 {
    public static void main(String[] args) {
        Integer i=new Integer(128);
        //转换成十进制
        String s=i.toBinaryString(55);
        //转换成八进制
        String s1=i.toOctalString(i);
        //转换成十六进制
        String s2=i.toHexString(i);
        System.out.println(s);
        System.out.println(s1);
        System.out.println(s2);
    }
}

(3)须要注意:包装类不能直接比较值, Integer默认值是nullide

public static void main(String[] args) {
    Integer i=new Integer(128);
    Integer j=new Integer(264);
    //包装类不能直接用"=="比较值,能够转换成基本数据类型再比较
    System.out.println(i.intValue()==j.intValue());
    //Integer中覆写了equal是方法,也能够直接用来比较两个Integer的值
    System.out.println(i.equals(j));
}
public class IntegerDemo {
    private String name;
    /*基本数据类型默认值是0
    * Integer默认值是null
    * */
    private Integer age;
    private Integer gender;
}

(4)自动装箱拆箱this

装箱:把int类型转换正Integer--------------》new Integer(int i)3d

拆箱:把Integer转换成int------------》intValue()对象方法指针

装箱和拆箱是不须要咱们主动调用的,是jvm自动给咱们完成的。orm

其余的7种基本数据类型和Integer同样。对象

public static void main(String[] args) {
    int i=5;
    //自动装箱
    Integer i1=i;

    Integer i2=new Integer(8);
    //自动拆箱
    int i3=i2;
}

自动装箱的底层原理,实际自动装箱也是建立Integer对象,只不过是Integer类自动完成,-128到127之间是使用缓存对象,超出这个范围都是new Integer(x)

public static void main(String[] args) {
    Integer i=127;
    Integer j=127;
    System.out.println(i==j);

    Integer i1=128;
    Integer j1=128;
    System.out.println(i1==j1);
}

 

65.日期类

日期的处理

import java.util.Date;

public class DateTest {
    public static void main(String[] args) {
        //得到当前的日期时间对象
        Date d1=new Date();
        System.out.println(d1);
        //得到从1970 年1月 1 日0点至今的毫秒数
        long c=System.currentTimeMillis();
        System.out.println(c);
        //根据毫秒数得到当前的日期时间对象
        Date d2=new Date(c-24*60*60*1000);
        System.out.println(d2);
        //从1970 年1月 1 日0点到d2这个日期时间的毫秒数
        long time=d2.getTime();
        System.out.println(time);
    }
}

66.日期的格式化 SimpleDateFormat类

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleDateFormatTest {
    public static void main(String[] args) {
        //得到当前日期时间
        Date d=new Date();
        //默认格式化
        SimpleDateFormat s1=new SimpleDateFormat();
        String d1=s1.format(d);
        System.out.println(d1);
        //指定格式化
        SimpleDateFormat s2=new SimpleDateFormat("yyyy-mm-dd hh:mm:ss E");
        String d2=s2.format(d);
        System.out.println(d2);
        //把字符串解析成日期的对象
        String s="2019-05-14 13:04:58 星期二";
        try {
            Date ds=s2.parse(s);
            System.out.println(ds);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

67.集合

数组:长度固定, 能够存储基本数据类型,也能存储对象

集合:长度可变,因为有包装类的存在,集合能够存储任何类型               

集合提供了丰富的API,操做集合更简单,功能更强大。

public static void main(String[] args) {
    //建立一个10个学生的集合
    Student[] students = new Student[10];
    //建立10个Student对象放在集合中
    for (int i = 0; i < students.length; i++) {
        students[i]=new Student("张三"+i,12030,18);
        //打印数组的时候是没有办法调用里面对象的方法的,因此用遍历的方式打印
        System.out.println(students[i]);
    }
}

(1)集合的体系

Collection接口是集合的根接口,橙色为最经常使用的

(2)接口 Collection<E>

E是泛型,指定往集合里面存什么类型

import java.util.ArrayList;
import java.util.Collection;

public class CollectionTest {
    public static void main(String[] args) {
        Collection floowers=new ArrayList();
        //在集合中添加元素
        floowers.add("芍药");
        floowers.add("百合");
        floowers.add("牡丹");
        floowers.add("雏菊");
        //集合中覆写了toString方法能够直接打印值
        System.out.println(floowers);
        Collection grass=new ArrayList();
        grass.add("含羞草");
        grass.add("灯芯草");
        //在集合中添加另外一个集合的内容
        floowers.addAll(grass);
        //从集合中移除单个元素
        floowers.remove("雏菊");
        //从集合中移除子集合的内容
        floowers.removeAll(grass);
        //判断集合中是否包含指定元素
        boolean a=floowers.contains("芍药");
        //判断集合中是否包含指定子集合
        boolean b=floowers.containsAll(grass);
        //判断集合是否为空
        boolean c=floowers.isEmpty();
        //清空集合
        grass.clear();
        //集合转化成数组,注意返回的是Object
        Object[] objects = floowers.toArray();
    }
}

(3)集合中的遍历---迭代器

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectionTest1 {
    public static void main(String[] args) {
        Collection floowers=new ArrayList();
        //在集合中添加元素
        floowers.add("芍药");
        floowers.add("百合");
        floowers.add("牡丹");
        floowers.add("雏菊");
        floowers.add("含羞草");
        floowers.add("灯芯草");
        iteratorCollection(floowers);
    }
    public static void iteratorCollection(Collection coll){
        //Iterator<E> iterator() 返回在此 collection 的元素上进行迭代的迭代器
        Iterator iterator = coll.iterator();
        //boolean hasNext()若是仍有元素能够迭代,则返回 true
        while(iterator.hasNext()){
            //E next()返回迭代的下一个元素 最好用Object接收
            Object next = iterator.next();
            System.out.println(next);
        }
    }
}

迭代器的指针是单向一去不复返的,在写遍历方法的时候只能用一次,到最后iterator.hasNext()的值是false就不能从头开始了

(4)集合的扩展(集合中的集合)

public class Book {
    private String name;
    private int price;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public Book(String name, int price) {
        this.name = name;
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}
import java.util.ArrayList;
import java.util.Collection;

public class Student {
    private String name;
    private int id;
    private int age;
    //学生类中有一个集合属性
    private Collection books=new ArrayList();

    public Student(String name, int id, int age) {
        this.name = name;
        this.id = id;
        this.age = age;
    }

    public Student(String name, int id, int age, Collection books) {
        this.name = name;
        this.id = id;
        this.age = age;
        this.books = books;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", id=" + id +
                ", age=" + age +
                ", books=" + books +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Collection getBooks() {
        return books;
    }

    public void setBooks(Collection books) {
        this.books = books;
    }
}
import java.util.ArrayList;
import java.util.Collection;

public class CollectionTest2 {
    public static void main(String[] args) {
        //建立一个学生集合
        Collection students=new ArrayList();
        //新建一个学生对象包含姓名、学号、年龄、藏书
        Student a1=new Student("张三",1,18);
        students.add(a1);
        //建立一个藏书集合为学生a1的藏书
        Collection books=a1.getBooks();
        //建立书籍对象
        Book b1=new Book("盗墓笔记",30);
        Book b2=new Book("绘意",15);
        //在集合中添加整个子集合做为内容
        books.add(b1);
        books.add(b2);
        Student a2 = new Student("李四", 2, 17);
        students.add(a2);
        //引用的全部类必须都覆写了toString方法才能打印出值来
        System.out.println(students);
    }
}

(5)迭代器的拓展

迭代器在遍历过程当中不容许变动集合

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectionTest3 {
    public static void main(String[] args) {
        Collection family=new ArrayList();
        family.add("mama");
        family.add("baba");
        family.add("i");
        iteratorCollection(family);
    }
    public static void iteratorCollection(Collection coll){
        Iterator iterator = coll.iterator();
        while(iterator.hasNext()){
            Object obj=iterator.next();
            System.out.println(obj);
            //迭代器在遍历过程当中不容许变动集合
            if(obj=="baba"){
                coll.add("papa");
            }
        }
    }
}

相关文章
相关标签/搜索