[英]convert object to string getting can not cast error
I have a list of object List in which at 4th index it has list of integer [1,2,3,4,5] Now I want to get list into a comma separated string. 我有一个对象列表List,其中在第4个索引处它具有整数[1,2,3,4,5]的列表。现在,我想将列表转换成逗号分隔的字符串。 below is my try, but it giving error as can not cast. 下面是我的尝试,但由于无法投射而产生错误。
for(Object[] objArr : relationshipInfo){
if(null != objArr[4]){
String groupedItemIds = (String)objArr[4];
}
how to do this? 这个怎么做?
Try the following:- use toString() 尝试以下操作:-使用toString()
String output = relationshipInfo.toString();
output = output.replace("[", "");
output = output.replace("]", "");
System.out.println(output);
[UPDATE] [UPDATE]
If you want fourth Object only then try: 如果只需要第四个对象,请尝试:
Object[] objArr = relationshipInfo.toArray();
String groupedItemIds = String.valueOf(objArr[4]);
Integer Array or Integer can not be cast to a String. 整数数组或整数不能强制转换为字符串。
try 尝试
for(Object[] objArr : relationshipInfo){
if(null != objArr[4]){
String groupedItemIds = new String (objArr[4]); // or String.valueOf(objArr[4]);
}
Update 更新
If the 4th index is a Array then try 如果第四个索引是数组,请尝试
String groupedItemIds = Arrays.asList(objArr[4]).toString();
which will give you a comma delimitered String 这将为您提供一个逗号分隔的字符串
You cannot cast an Object to an uncomatible type 您不能将对象强制转换为不兼容的类型
for(Object[] objArr : relationshipInfo){
if(null != objArr[4]){
List<Integer> groupedItemIds = (List<Integer)objArr[4];;
//Loop over the integer list
}
You cannot type cast Object to String unless the Object is indeed a String. 除非对象确实是字符串,否则不能将类型转换为字符串。 Instead you can do the following - 相反,您可以执行以下操作-
Call toString()
on it. 调用toString()
。 Override it in your class. 在您的课程中覆盖它。
Try this : 尝试这个 :
for(Object[] objArr : relationshipInfo)
{
if(null != objArr[4])
{
String groupedItemIds = String.valueOf(objArr[4]);
}
}
Ref : 参考:
public static String valueOf(Object obj)
Returns the string representation of the Object argument. 返回Object参数的字符串表示形式。
Link . 链接 。
Difference between use of toString()
and String.valueOf()
使用toString()
和String.valueOf()
之间的区别
if you invoke toString()
with a null
object or null value, you'll get a NullPointerExcepection
whereas using String.valueOf()
you don't have to check for null
value. 如果使用null
对象或空值调用toString()
,则将获得NullPointerExcepection
而使用String.valueOf()
则不必检查null
值。
you want "comma separated string" . 您想要“逗号分隔的字符串”。 So, you iterate over the 4th index and read each integer and do "" + int1 +" , " + int2 etc.. you can do this (in) by overriding your toString() method.. 因此,您遍历第4个索引并读取每个整数,然后执行““ + int1 +”,“ + int2等。”您可以通过重写toString()方法来执行此操作。
You could try: 您可以尝试:
String groupedItemIds = Arrays.asList( objArr[4] ).toString();
This will produce: groupedItemIds = "[1,2,3,4,5]"
这将产生: groupedItemIds = "[1,2,3,4,5]"
看起来您使用的是数组而不是列表,在这种情况下可以使用:
String groupedItemIds = java.util.Arrays.toString(objArr[4]);
I resolved my problem by belwo code 我通过belwo代码解决了我的问题
byte[] groupedItemIdsArr = (byte[])objArr[4];
String groupedItemIds = new String(groupedItemIdsArr);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.