英文:
Special Scenario : java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
问题
我只是想尝试解决以下异常。
java.lang.ClassCastException: [Ljava.lang.Object;无法转换为[Ljava.lang.String;
请在这里帮助我。
我需要将其转换为List<String[]>。我应该如何处理?
List<Object[]> values = query.getResultList();
List<String[]> strings = new ArrayList<>();
for (Object[] object : values) {
strings.add((String[]) object);
}
> values := [obj1,obj2,obj3]
>
> obj1 = "1","01";
> obj2 = "1","02";
> obj3 = "1:,"03";
英文:
I am simply trying to remove below exception.
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
Please help me out here.
I need to cast it as List<String[]>. How should i handle it?
List<Object[]> values = query.getResultList();
List<String[]> strings = new ArrayList<>();
for (Object[] object : values) {
strings.add((String[]) object);
}
> values : = [obj1,obj2,obj3]
>
> obj1 = "1", "01";
> obj2 = "1", "02";
> obj3 = "1:, "03";
答案1
得分: 0
我认为你应该使用 System.arraycopy
package com.lsk.backend;
import java.util.*;
import java.util.List;
/**
* @ClassName : Test //className
* @Description : //description
* @Author : shikai.liu //author
* @Date: 2020-08-27 09:13 //Date
*/
public class Test {
public static void main(String[] args) {
List<Object[]> values = getResultList();
List<String[]> strings = new ArrayList<>();
for (Object[] object : values) {
String[] dst = new String[object.length];
System.arraycopy(object, 0, dst, 0, dst.length);
strings.add(dst);
}
System.out.println(strings);
}
public static List<Object[]> getResultList() {
Object[][] result = {
{
"0", "1"
},
{
"0", "2"
},
{
"0", "3"
},
};
List<Object[]> ss = Arrays.asList(result);
return ss;
}
}
英文:
I think you should use the System.arraycopy
package com.lsk.backend;
import java.util.*;
import java.util.List;
/**
* @ClassName : Test //className
* @Description : //description
* @Author : shikai.liu //author
* @Date: 2020-08-27 09:13 //Date
*/
public class Test {
public static void main(String[] args) {
List<Object[]> values = getResultList();
List<String[]> strings = new ArrayList<>();
for (Object[] object : values) {
String[]dst=new String[object.length];
System.arraycopy(object,0,dst,0,dst.length);
strings.add(dst);
}
System.out.println(strings);
}
public static List<Object[]>getResultList(){
Object[][]result={
{
"0","1"
},
{
"0","2"
},
{
"0","3"
},
};
List<Object[]>ss=Arrays.asList(result);
return ss;
}
}
答案2
得分: 0
List<Object[]> values = query.getResultList();
List<String[]> strings = new ArrayList<>();
values.forEach(object -> {
String[] stringArray = new String[object.length];
System.arraycopy(object, 0, stringArray, 0, object.length);
strings.add(stringArray);
});
英文:
List<Object[]> values = query.getResultList();
List<String[]> strings = new ArrayList<>();
values.forEach(object -> {
String [] stringArray = new String[object.length];
System.arraycopy(object, 0, stringArray, 0,object.length);
strings.add(stringArray);
});
</details>
# 答案3
**得分**: 0
这对我最终起作用了
values.forEach(o-> { strings.add(Arrays.copyOf(o, o.length, String[].class)); });
<details>
<summary>英文:</summary>
This worked for me finally
values.forEach(o->{ strings.add(Arrays.copyOf(o, o.length, String[].class));});
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论