【Java】ArrayList<String>转化为String数组问题

Java的容器类Collections中toArray()方法,可以把诸如ArrayList<String>的动态数组、不定长转化静态数组、定长数组String[]

但是,如下的转化方式是错误的。

String[] strArray = (String[]) arrayList.toArray();

如果这样执行会导致如下的错误:

Exception in thread "xx" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;

如下图:

正确的方式应该写成:

String[] strArrayTrue = (String[]) arrayList.toArray(new String[0]);

比如,如下的一段程序,是正常的:

import java.util.*;

public class ArrayListToStrArr {

	public static void main(String[] args) {
		ArrayList<String> arrayList = new ArrayList<String>();
		arrayList.add("1");
		arrayList.add("2");
		// 正确转化方式
		String[] strArrayTrue = (String[]) arrayList.toArray(new String[0]);
		for (String str : strArrayTrue) {
			System.out.println(str);
		}
		// 错误的转化方式
		// String[] strArray = (String[]) arrayList.toArray();

	}

}

同理推广到数组ArrayList<Integer>等

时间: 2024-08-27 02:11:19

【Java】ArrayList<String>转化为String数组问题的相关文章

Java判断回文语句的程序(可变参数,String转化为char数组)

static void Huiwen(char... cs){     //char... cs  支持可变参数格式为 //(类型名... 变量名)--形参列表,相当于建立了一个长度可变的动态数组,系统根据用户需求来确定数组的长度 int b_ool=1; for(int i=0;i<cs.length/2;i++)    //length为这个可变数组的长度,注意此时长度为数组下表加1,通过cs.length-i-1可知 if(cs[i]!=cs[cs.length-i-1   // 判断 ]

如何将List&lt;string&gt;转化为string

Convert List, string. A List can be converted to a string. This is possible with the ToArray method on the List type. We can also convert a string into a List.ConversionsThe StringBuilder type helps with certain conversions, which are done with loops

java中InputStream转化为byte[]数组

//org.apache.commons.io.IOUtils.toByteArray已经有实现 String filePath = "D:\\aaa.txt"; in = new FileInputStream(new File(filePath)); fileBytes =IOUtils.toByteArray(in); 版权声明:本文为博主原创文章,未经博主允许不得转载.

Java 带分隔字符串、字符串数组和 ArrayList&lt;String&gt; 之间的转换

太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. 一.先来认识一下标题说的这三件东西,也许描述的不清楚,但有了下面的例子,就不会有歧义了 1.带分隔字符串是这样的: String seperate

java中如何将char数组转化为String

1.直接在构造String时建立. char data[] = {'s', 'g', 'k'}; String str = new String(data); 2.String有方法可以直接转换. String.valueOf(char[] chr)就可以. 如: char[] cha = {'s','g','h'}; String n = String.valueOf(char[]); String.valueOf()函数支持将boolean ,char,char[],double,float

JavaSE8基础 String 通过构造方法 将一维char数组化为String

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0)        code: package jizuiku.t01; public class Demo { public static void main(String[] args) { char[] data = { 'a', 'b', 'c' }; String str = new String(data); System.

JavaSE8基础 String 通过构造方法 将一维byte数组化为String

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0)        code: package jizuiku.t01; public class Demo1 { public static void main(String[] args) { byte[] b = { 97, 98, 99, 100 };//这里面存储的是 ascii码,97对应的字符是a String str =

leetcode 186. Reverse Words in a String II 旋转字符数组 ---------- java

Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters. The input string does not contain leading or trailing spaces and the words are always separated by a single space. For example,Given s =

将fastjson元素转化为String[]

在fastjson中如果JSONObject中添加了 String[] 类型的元素 例如 JSONObject jo = new JSONObject(); String[] array = {"1", "2"}; jo.put("array", array); 将JSONObject中String[]提取出来需要 (String[])(((JSONArray)jo.get("array")).toArray(new Stri