2020-11-26-Java-复习-53(java工具箱类1)

2020-12-02  本文已影响0人  冰菓_

1.org.apache.commons.beanutils.BeanUtils

(避免使用copyProperties进行拷贝)

public class Test1 {
    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException, InstantiationException {
        User user = new User();
        BeanUtils.setProperty(user,"name","xioah");
        BeanUtils.setProperty(user,"sex",'男');
        System.out.println(user);//User{name='xioah', age=0, sex=男}
        User user1 = new User();
        BeanUtils.copyProperties(user1,user);//User{name='xioah', age=0, sex=男}
        System.out.println(user1);
        String name = BeanUtils.getProperty(user1, "name");
        System.out.println(name);//xioah
        User user2 = (User) BeanUtils.cloneBean(user1);
        System.out.println(user2);
    }
}

2.org.apache.commons.lang3.ArrayUtils

import org.apache.commons.lang3.ArrayUtils;

import java.util.Arrays;

public class Test2 {
    public static void main(String[] args) {
        String[] st = new String[]{};
        //添加元素,原来的数组是final copy了原来的数组
        String[] as = ArrayUtils.add(st, "a");
        String[] bs = ArrayUtils.add(as, "b");
        System.out.println(Arrays.toString(bs));
        //由于判断是否包含
        boolean cont = ArrayUtils.contains(bs, "a");
        System.out.println(cont);
        //addAll也是copy了原来的数组
        String[] strings = ArrayUtils.addAll(bs, new String[]{"C", "D"});
        System.out.println(Arrays.toString(strings));
        System.out.println(Arrays.toString(bs));
        //克隆一个数组
        String[] clone = ArrayUtils.clone(bs);
        System.out.println(Arrays.toString(clone));
        //比较数组是否相等,
       // ArrayUtils.isEquals()
        int[] ints = new  int[]{};
        //把基础类型转化为包装类
        Integer[] integers = ArrayUtils.toObject(ints);
        //查找元素,存在返回数组下标,否则返回-1
        int i = ArrayUtils.indexOf(bs, "a");
        System.out.println(i);
        //reverse反装数组,在自身上反转
        ArrayUtils.reverse(bs);
        System.out.println(Arrays.toString(bs));
    }
}

上一篇下一篇

猜你喜欢

热点阅读