返回值类型是数组的操作

/**

  • @author lg

  • @Description 对于数组的判断,除了反射,还可以这样子来进行操作

  • @date 2022/1/14 9:52
    */
    public class SwitchTest {

    public static void main(String[] args) {
    try {
    Object obj = getObj("110");
    if (obj.getClass().isArray()){
    System.out.println("hello,这是一个数组 ");
    int length = Array.getLength(obj);
    if (length == 0) {
    throw new IllegalAccessException("数组长度为0");
    } else {
    for (int i = 0; i < length; i++) {
    Object o = Array.get(obj, i);
    // 输出每个参数的值即可
    System.out.println(o);
    }
    }
    }
    } catch (IllegalAccessException e) {
    e.printStackTrace();
    }

    }

    public static Object getObj(String code) throws IllegalAccessException {
    Object object;
    switch (code) {
    case "110":{
    object = new String[]{"1", "2", "3"};
    break;
    }
    case "111":{
    object = new String[]{"3", "4", "5"};
    break;
    }
    default:
    object = "hello,world";
    throw new IllegalAccessException("参数不合理异常");
    }
    return object;
    }
    }

posted @ 2022-01-14 10:21  写的代码很烂  阅读(104)  评论(0编辑  收藏  举报