Type cast variable to String in python

This can be done by using str() function.

For example:

myvar = str(myvar)

Advertisements

Casting Array in Java

Casting an array directly throw ClassCastException. This is how I get around it.

Basic:

import java.util.ArrayList;

ArrayList<MyObject> tmp = new ArrayList<MyObject>();

// insert data in to tmp

// this will prompt and ask for casting

MyObject[] tmpArray = tmp.toArray();

// this will throw ClassCastException

MyObject[] tmpArray (MyObject[]) tmp.toArray();

Why? Because toArray() returns a type of Object[] and it requires type casting.

How?

MyObject[] tmpArray (MyObject[]) tmp.toArray(new MyObject[tmp.size()]);

I guess it should be the same for other dynamic list types.