Java学习与复习笔记--Day07

包装类:

基本数据类型,使用起来非常方便,但是没有对应的方法来操作这些数据,可以使用一个类,把基本数据类型的数据包装起来,在类中定义一些方法,这个类叫做包装类,我们可以使用类中的方法来操作这些基本类型的数据

/*
* 装箱:把基本类型的数据,包装到包装类中(基本类型的数据->包装类)
* 构造方法:Integer(int value)
  构造一个新分配的 Integer对象,该对象表示指定的 int值。
  Integer(String s)
  构造一个新分配 Integer对象,表示 int由指示值 String参数。
  传递的字符串,必须是基本类型的字符串,否则会抛出异常“100”正确,“a”抛异常
  静态方法:static Integer valueOf(int i)
  返回一个 Integer指定的 int值的 Integer实例。
  static Integer valueOf(String s)
  返回一个 Integer对象,保存指定的值为 String。
  拆箱:在包装类中取出基本类型的数据(包装类->基本类型的数据)
  成员方法:int intValue()
  将Integer的值作为int 。

*/
public class Demo01Integer {
    public static void main(String[] args) {
        //装箱
        //构造方法
        Integer in1 = new Integer(1);//方法上有横线,说明方法过时了
        System.out.println(in1);//重写了toString方法
        Integer in2 = new Integer("1");
        System.out.println(in2);
        //静态方法
        Integer in3 = Integer.valueOf(1);
        System.out.println(in3);
//        Integer in4 = Integer.valueOf("a");//数字格式化异常
//        System.out.println(in4);
    }
}
/*
* 自动装箱和自动拆箱:基本类型的数据和包装类之间可以自动的相互转换
* JDK1.5之后的新特性*/
public class Demo02Integer {
    public static void main(String[] args) {
        /*
        * 自动装箱:直接把int类型的整数赋值包装类
        * Integer in=1;相当于Integer in=new Integer(1);
        * */
        Integer in=1;
        /*
        * 自动拆箱:in是包装类,无法直接参与运算,可以自动转换为基本数据类型,再进行计算
        * in+2;相当于in.intValue()+2;
        * in=in.intValue()+2=3;又是一个自动装箱*/
        in=in+2;
        ArrayList<Integer> list=new ArrayList<>();
        /*
        * ArrayList集合无法直接存储整数,可以直接存储Integer包装类
        * */
        list.add(1);//隐含了自动装箱 list.add(new Integer(1));
        int a = list.get(0);//隐含了自动拆箱,list.get(0).intValue();
    }
}
/*
* 基本类型与字符串类型之间的相互转换
* 基本类型->字符串(String)
* 1.基本类型的值+""最简单的方法(工作中常用)
* 2.包装类的toString(参数),不是Object类的toString()重载
* static String toString(int i)
  返回一个 String指定整数的 String对象。
  3.String类的静态方法valueof(参数)
  static String valueOf(int i)
  返回int参数的字符串int形式。
  字符串(String)->基本类型
  使用包装类的静态方法parseXXX("字符串");
  Integer类:static int parseInt(String s)
  double类:static double parseDouble(String s)*/
public class Demo03Integer {
    public static void main(String[] args) {
//        基本类型->字符串(String)
        int i1=100;
        String s1=i1+"";
        System.out.println(s1+200);
        String s2 = Integer.toString(100);
        System.out.println(s2+200);
        String s3 = String.valueOf(100);
        System.out.println(s3+200);
//        字符串(String)->基本类型
        int i = Integer.parseInt(s1);
        System.out.println(i-10);

        int a = Integer.parseInt("a");//数字格式化异常
        System.out.println(a);
    }
}

Collection集合

集合:集合是java中提供的一种容器,可以用来存储多个数据。

集合与数组的区别:

数组长度时固定的,集合的长度是可变的。

数组中存储的是同一类型的数据,可以存储基本数据类型值。集合存储的都是对象,而且对象的类型可以不一致。在开发中一般当对象多的时候,可以用集合进行存储。

Collection接口

1.定义的是所有单列集合中共性的方法

2.所有的单列集合都可以使用共性的方法

3.没有带索引的方法

List集合

1.有序的集合(存储和取出元素顺序相同)

2.允许存储重复的元素

3.有索引,可以使用普通的for循环遍历

继承:子类共性抽取,形成父类(接口)

Vector集合、ArrayList集合、LinkedList集合

Set接口

1.不允许存储重复元素

2.没有索引(不能使用普通的for循环遍历)

TreeSet集合(无序的集合(存储和取出元素的顺序有可能不一致))、HashSet集合(无序的集合(存储和取出元素的顺序有可能不一致))->LinkedHashSet集合(有序的集合)

Collection的常用功能:

/*
* java.util.Collection接口
* 所有单列集合的最顶层的接口,里面定义了所有单列集合共性的方法
* 任意的单列集合都可以使用Collection接口中的方法
* 共性的方法:
* boolean add(E e) :确保此集合包含指定的元素(可选操作)。
* void clear() :从此集合中删除所有元素(可选操作)。
* boolean remove(Object o) :从该集合中删除指定元素的单个实例(如果存在)(可选操作)。
* boolean contains(Object o) :如果此集合包含指定的元素,则返回 true 。
* boolean isEmpty() :如果此集合不包含元素,则返回 true
* int size() :返回此集合中的元素数。
* Object[] toArray() :返回一个包含此集合中所有元素的数组。 */
public class Demo01Collection {
    public static void main(String[] args) {
        //创建一个集合对象,可以使用多态
        //Collection<String> coll=new ArrayList<>();
        Collection<String> coll=new HashSet<>();//多态,也可以直接改为HashSet,直接继承顶层特性,表现出相应的特点
        System.out.println(coll);//重写了toString方法,打印为[]

        /*
        * public boolean add(E e):把给定的对象添加到当前的集合中。
        * 返回值是一个boolean值,一般都返回true,所有可以不用接收*/
        boolean b1=coll.add("张三");
        System.out.println("b1:"+b1);//b1:true
        System.out.println(coll);//[张三]
        coll.add("李四");
        coll.add("李四");
        coll.add("王五");
        coll.add("赵柳");
        coll.add("田七");
        System.out.println(coll);//[张三, 李四, 王五, 赵柳, 田七]

        /*
        *public boolean remove(Object o) :把给定的对象在当前集合中删除。
         返回值是一个boolean值,集合中存在元素,删除元素,返回true,
                              集合中不存在元素,删除失败,返回false
         */
        boolean b2 = coll.remove("赵柳");
        System.out.println("b2:"+b2);
        boolean b3 = coll.remove("赵四");
        System.out.println("b3:"+b3);//b3:false
        System.out.println(coll);//[张三, 李四, 王五, 田七]

        /*public boolean contains(Object o) :判断当前集合中是否包含给定的对象,
                                        如果此集合包含指定的元素,则返回 true
                                        不包含,返回false
        */
        boolean b4 = coll.contains("李四");
        System.out.println("b4:"+b4);//b4:true
        boolean b5 = coll.contains("赵四");
        System.out.println("b5:"+b5);//b5:false
        //public boolean isEmpty() :判断当前集合是否为空,如果此集合不包含元素,则返回true,否则返回false
        boolean b6 = coll.isEmpty();
        System.out.println("b6:"+b6);//b6:false
        //public int size() :返回此集合中的元素数。
        int b7 = coll.size();
        System.out.println("b7:"+b7);//b7:4
        //public Object[] toArray() :把集合中的元素,存储到数组中
        Object[] arr = coll.toArray();
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
        //public void clear() :清空集合中所有的元素。但是不删除集合,集合还存在
        coll.clear();
        System.out.println(coll);//[]
        System.out.println(coll.isEmpty());//true
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值