Hello,大家好!我是程序员阿飞!今天主要给大家分享一下Java中集合与数组之间的转换,好了,我们直奔主题吧!

    一、集合转数组

        ArrayList list = new ArrayList();

        list.add("dff1");

        list.add("dff2");

        list.add("dff3");

        list.add("dff4");

        list.add("dff5");

        String[] array = new String[list.size()];

        String[] s=list.toArray(array);

        System.out.println(Arrays.toString(s));

        最好用 new 的方式构建,因为 list.toArray() 默认转换为 Object[] 对象。      

     List detailList = taskDao.findDetailList(taskId);

       if(detailList.size()>0){

          GJTaskThemeCategoryDetail [] details = detailList.toArray( new GJTaskThemeCategoryDetail [] {});

          return details;

        }

    二、数组转集合

  1. String[] string=new String[]{"s1","s2","s3","s4","s5"};

        List li= Arrays.asList(string);

        System.out.println(li);