csdnceshi62 2010-10-28 11:30 采纳率: 100%
浏览 931
已采纳

在 Java 中将 ArrayList 字符串转换为 String []

How might I convert an ArrayList<String> object to a String[] array in Java?

转载于:https://stackoverflow.com/questions/4042434/converting-arrayliststring-to-string-in-java

  • 写回答

17条回答 默认 最新

  • bug^君 2010-10-28 11:34
    关注
    List<String> list = ..;
    String[] array = list.toArray(new String[0]);
    

    For example:

    List<String> list = new ArrayList<String>();
    //add some stuff
    list.add("android");
    list.add("apple");
    String[] stringArray = list.toArray(new String[0]);
    

    The toArray() method without passing any argument returns Object[]. So you have to pass an array as an argument, which will be filled with the data from the list, and returned. You can pass an empty array as well, but you can also pass an array with the desired size.

    Important update: Originally the code above used new String[list.size()]. However, this blogpost reveals that due to JVM optimizations, using new String[0] is better now.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(16条)

报告相同问题?

悬赏问题

  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 51单片机显示器问题