2021-06-04

day11

Collection接口

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
​
public class CollectionTest {
    @SuppressWarnings("all")
    public static void main(String[] args) {
        Collection c = new ArrayList();
        //添加元素
        c.add("hello1");
        c.add("hello2");
        c.add("hello3");
        c.add(1);
        System.out.println(c.size());
        System.out.println(Arrays.toString(c.toArray()));
        System.out.println(c);
        Collection c1 = new ArrayList();
        c1.add("hello4");
        c.addAll(c1);
        System.out.println(c.size());
        System.out.println(Arrays.toString(c.toArray()));
        //c.clear();
        c.remove("hello4");
        
        Iterator iterator = c.iterator();
        while(iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        // foreach 循环
        for(Object o:c) {
            System.out.println(o);
        }
        
        
        System.out.println(c);
        System.out.println(c.isEmpty());
    }
​
}

装箱拆箱

public class IntegerTest {
    public static void main(String[] args) {
        int i = 1;
        //装箱,1.5之前
        Integer integer2 = new Integer(i);
        Integer integer3 = Integer.valueOf(i);
        //1.5之后
        Integer integer = 1;
        System.out.println(integer.getClass());
        System.out.println(integer2.toString());
        System.out.println(integer.MAX_VALUE);//2的31次
        System.out.println(integer.MIN_VALUE);//-2的31次
        System.out.println(integer.toString(100));
        System.out.println(integer.toString(8,2));//8转2进制
        System.out.println(integer.toString(100,16));//100转16进制
        
        
        //拆箱
        //1.5之前
        int a = integer2.intValue();
        System.out.println(a);
        //1.5之后,自动拆箱
        int b = integer;
        System.out.println(b);
    }
}

equals重写

public class Student {
    String name = "wzh";
    int age = 12;
    public Student(String name,int age) {
        this.name = name;
        this.age = age;
        
    }
    @Override
    public String toString() {
        return "Student [name ="+ name + ",age=" + age +"]";
    }
    @Override
    public boolean equals(Object obj) {
        if (obj==null) {
            return false;
        }
        if (obj instanceof Student) {
            return true;
        }
        Student s = (Student)obj;
        //this  obj
        if (this.name.equals(s.name) && this.age == s.age) {
            return true;    
        }
        return false;
    }
    public static void main(String[] args) {
        Student student = new Student("tom",18);
        //System.out.println(student.toString());
        System.out.println(student);
        Student student2 = new Student("tom", 18);
        System.out.println(student.equals(student2));
        
        //equals  ==
        //== 操作符
        //基本类型:值    引用类型:地址值
        //equals 方法
        //引用类型:没有重写方法:地址值      重写:根据重写的规则比较
    }
​
}

sort排序

import java.util.Arrays;
​
public class Test {
    public void sort(int[] arr,Algorithm algo) {
        algo.sort(arr);
    }
    public static void main(String[] args) {
        Algorithm alg = (int[] arr) ->Arrays.sort(arr);
        /*Algorithm alg = new Algorithm() {
            
            @Override
            public void sort(int[] arr) {
                Arrays.sort(arr);
                //冒泡---选择---插入
                
            }
        };*/
        int[] arr = {3,6,1,9};
        System.out.println("before" + Arrays.toString(arr));
        alg.sort(arr);
        System.out.println("after" + Arrays.toString(arr));
    }
}

==应用

public class SuperTest {
    public static void main(String[] args) {
        String s1="helloworld"; 
        String s2="hello"; 
        String s3="world";
        String s4="hello"+"world"; 
        String s5=s2+s3;
        String s6=new String("helloword"); 
        String s7="hello"+new String("world"); 
        System.out.println(s1==s4);                 
        System. out. println(s1==s5);               
        System. out. println(s1==s6);               
        System. out. println(s1==s7);
    
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值