A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 时光♪微凉 于 2014-6-18 23:21 编辑
  1. public static void main(String[] args) {
  2.                 List<String> str = new ArrayList<String>();
  3.                 str.add("11");
  4.                 str.add("22");
  5.                 str.add("33");
  6.                 String[] arrays = toArrays(str);
  7.                 for (String string : arrays) {
  8.                         System.out.println(string);
  9.                 }
  10.         }
  11.         
  12.         public static <T> T[] toArrays(List<T> list){
  13.                 if(list==null)
  14.                         return null;
  15.                 @SuppressWarnings("unchecked")
  16.                 T[] t = (T[])new Object[list.size()];
  17.                 for (int i = 0; i < list.size(); i++) {
  18.                         t[i]=list.get(i);
  19.                         System.out.println("Why");
  20.                 }
  21.                 return t;
  22.         }
复制代码

4 个回复

正序浏览
楼主看下,现在求积分啊,一份汗水一份收获啊,版主是帅哥(美女)哦,给点分奥
回复 使用道具 举报
package IO;

import java.util.ArrayList;
import java.util.List;

public class Something {
        public static void main(String[] args) {
                List<String> str = new ArrayList<String>();
                str.add("11");
                str.add("22");
                str.add("33");
                //这里传入String数组
                String[] arrays = str.toArray(new String[0]); // 重点在这,查询API文档,
                for (String string : arrays) {
                        System.out.println(string);
                }

        }
        //我的思路,这里可以删掉,没有运行这里的代码
        /*public static <T> T[] toArrays(List<T> list) {
                if (list == null)
                        return null;
                @SuppressWarnings("unchecked")
                T[] t = (T[]) new Object[list.size()];
                for (int i = 0; i < list.size(); i++) {
                        t[i] = list.get(i);
                        System.out.println("Why");
                }
                return t;
        }*/
}

/*   加了多行注释后,一格式化成这样了,手工解开吧  是按你的愿意修改的
* public static void main(String[] args) { List<String> str = new
* ArrayList<String>(); str.add("11"); str.add("22"); str.add("33"); String[]
* arrays = toArrays(str); for (String string : arrays) {
* System.out.println(string); }
*
*
* }
*
* public static String[] toArrays(List<String> list) {
*
* if (list == null)
*
* return null;
*
* @SuppressWarnings("unchecked") String[] t = new String[list.size()]; for (int
* i = 0; i < list.size(); i++) { t[i] = list.get(i); System.out.println("Why");
* } return t; }
*
* }
*/

评分

参与人数 1技术分 +1 收起 理由
天涯追梦 + 1 赞一个!

查看全部评分

回复 使用道具 举报
chenzhiyuan 发表于 2014-6-1 17:42
是对的啊,怎么提问了啊,

编译是对的,但是运行报错了。ClassCastException异常
回复 使用道具 举报
是对的啊,怎么提问了啊,
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马