0

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. 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?

2
  • Do you know that each object is having toString() method? Commented Dec 18, 2013 at 6:22
  • you are doing it the wrong way 1/ why for loop when you know you want fourth element 2/ use toArray() instead 3/ Cannot type cast Integer to String | use String.valueOf() Commented Dec 18, 2013 at 7:17

9 Answers 9

1

Try the following:- use toString()

String output = relationshipInfo.toString();
output = output.replace("[", "");
output = output.replace("]", "");
System.out.println(output);


[UPDATE]

If you want fourth Object only then try:

    Object[] objArr = relationshipInfo.toArray();
    String groupedItemIds = String.valueOf(objArr[4]);
Sign up to request clarification or add additional context in comments.

4 Comments

relationshipInfo is an array of which he wants the 4th index.
I tried some to sugessions but always getting java.lang.ClassCastException: [B cannot be cast to java.lang.String exception.
@user2310289 u can upVote now :D neways thanx
I resolved by byte[] groupedItemIdsArr = (byte[])objArr[4]; String groupedItemIds = new String(groupedItemIdsArr);
0

You cannot type cast Object to String unless the Object is indeed a String. Instead you can do the following -

Call toString() on it. Override it in your class.

Comments

0

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..

Comments

0

You could try:

String groupedItemIds = Arrays.asList( objArr[4] ).toString();

This will produce: groupedItemIds = "[1,2,3,4,5]"

Comments

0

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.
Link.

Difference between use of toString() and 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.

Comments

0

It looks like you are using arrays not Lists in which case you can use:

String groupedItemIds = java.util.Arrays.toString(objArr[4]);

Comments

0

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
}

Comments

0

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

Comments

0

I resolved my problem by belwo code

byte[] groupedItemIdsArr = (byte[])objArr[4];
String groupedItemIds = new String(groupedItemIdsArr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.