返回值类型是数组的操作

/**

  • @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 @   雩娄的木子  阅读(113)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示