when casting is need for clone() method? - java

consider the code below:
ArrayList<Double> list1 = new ArrayList<>();
list1.add(1.5);
list1.add(2.5);
list1.add(3.5);
ArrayList<Double> list2 = (ArrayList<Double>)list1.clone();
Date[] list3 = {new Date(), new Date(4664316)};
Date[] list4 = list3.clone();
int[] list5 = {1, 2};
int[] list6 = list5.clone();
why list.clone() requires casting, while list3.clone() and list5.clone() don't need casting? I know the difference is between array and ArrayList, but not sure exactly why.

Please read the documentation.
Note that all arrays are considered to implement the interface Cloneable and that the return type of the clone method of an array type T[] is T[] where T is any reference or primitive type.
But in ArrayList, an Object is returned, so a cast is needed.

Clone() method signature returns an object type.
By convention, the object returned by this method should be independent of this object (which is being cloned)
Hence the cast is required.
Arrays behave differently and return a correctly typed clone array. So no casting required.

Related

Cast object array to generic array

Currently, I am viewing the source code of java.util.ArrayList. Now I find the function public void ensureCapacity(int minCapacity) casts an object array to a generic array, just like code below:
E[] newData = (E[]) new Object[Math.max(current * 2, minCapacity)];
However, when I declare the array to a specific type, IDE will show an error.
Object[] arr = new Object[10];
int[] arr1 = (int[]) new Object[arr.length];
Any one is able to tell me the differences between them? Thanks a lot.
It's because E (in the source code of ArrayList) stands for some reference type, but not for some primitive type.
And that's why you get a compile-time error when trying to cast an array of Object instances to an array of primitives.
If you do (for example)
Object[] arr = new Object[10];
Integer[] arr1 = (Integer[]) new Object[arr.length];
the error will be gone.
You can never cast a reference type (anything that extends from Object) to a primitive type (int, long, boolean, char, etc.).
You can also not cast an array of a reference type like Object[] to an array of a primitive type like int[].
And primitives cannot stand in for a generic parameter.
int is not Object, but it's primitive.
Use Integer and it will work.
Object[] arr = new Object[10];
Integer[] arr1 = (Integer[]) new Object[arr.length];

Why java does not autobox int[] to Integer[]

When I do the following,
arrayList1 - contains one element and it is an int[].
arrayList2 - not compiling (Error : The constructor ArrayList<Integer>(List<int[]>) is undefined)
arrayList3 - contains 7 elements and they are Integer objects
Here's the code:
int[] intArray = new int[]{2,3,4,5,6,7,8};
ArrayList arrayList1 = new ArrayList(Arrays.asList(intArray));
ArrayList<Integer> arrayList2 = new ArrayList<Integer>(Arrays.asList(intArray));
Integer[] integerArray = new Integer[]{2,3,4,5,6,7,8};
ArrayList<Integer> arrayList3 = new ArrayList<Integer>(Arrays.asList(integerArray));
Question :
Why doesn't the compiler auto-box the elements in the int[] to Integer and create an ArrayList<Integer>? What is the reason behind this? Is that my stupidity or some other reason?
The difference is int[] is itself an Object, whereas Integer[] is an array of references to Integer object.
Arrays.asList(T...) method takes variable arguments of some type T with no upper bounds. The erasure of that method is Arrays.asList(Object...). That means it will take variable number of arguments of any type that extends from Object.
Since int is not an Object, but a primitive type, so it can't be passed as individual element of T[], whereas int[] is an Object itself, it will go as first element of the T[] array (T... internally is a T[] only). However, Integer[] will be passed as T[], with each reference in Integer[] passed as different argument to T[].
And even if you would argue that compiler should have done the conversion from each element of int[] array to Integer, well that would be too much work for the compiler. First it would need to take each array element, and box it to Integer, then it would need to internally create an Integer[] from those elements. That is really too much. It already has a direct conversion from int[] to Object, which it follows. Although I have always wished Java allowed implicit conversion from int[] to Integer[], that would have made life simpler while working with generics, but again, that's how the language is designed.
Take a simple example:
Object[] array = new Integer[10]; // this is valid conversion
Object[] array2 = new int[10]; // this is not
Object obj = new int[10]; // this is again a valid conversion
So, in your code Arrays.asList(intArray) returns a ArrayList<int[]> and not ArrayList<Integer>. You can't pass it to the ArrayList<Integer>() constructor.
Related:
int[] and Integer[]: What is the difference?
An int[] is not the same as an Integer[].
An array has as associated Class object. The class object for an array of primitive ints is [I. The class object for an array of Integer is [Ljava/lang/Integer.
An array is itself an object, so converting between two objects of the same type is an identity conversion. Converting between two different typed objects isn't - and int[] and Integer[] are definitely different, as evidenced by the bytecode above.
Lastly, bear in mind that autoboxing would only really apply if there was an associated boxing conversion.
Technically it is possible to do it of course. However autoboxing/unboxing of primitive type array to wrapper type array is more than what you expect.
First look into the auto-boxing/unboxing of Java: What it does is simply a syntax sugar to save you typing the primitive wrapper code. e.g.
Integer i = 10;
Compiler knows that it is expecting an Integer, but int present instead. Therefore what the compiler doing is translating your code to:
Integer i = Integer.valueOf(10);
It does similar thing for unboxing: when in situation that it expects int but Integer is present, compiler replace it with varName.intValue()
Back to array. There are two problems we can forsee:
The first problem is, there is no straight-forward way to transform from an int array to an Integer array. You may argue that the compiler can transform
int[] intArray = ....;
Integer[] wrapperArray = intArray ;
to
Integer[] wrapperArray = new Integer[intArray.size()];
for (int i = 0; i < intArray.size(); i++) {
wrapperArray[i] = Integer.valueOf(intArray[i]);
}
but that seems too much for a syntax sugar.
The second big problem is, when you are passing it as a parameter to a method, if autoboxing/unboxing happens for array, instead of reference of original array is passed, you are now passing the reference of a copy of the original array. In case you are changing the content of array in your method, the original array will not be affected. That can bring you lots of surprises.
e.g.
void foo(Integer[] arr) {
arr[0] = 0;
}
// invoking foo in some code:
int[] intArr = new int[]{9,8,7,6};
foo(intArr);
// intArr[0] will still be 9, instead of 0
Because int[] and Integer[] both are objects. First will hold primitive int values, which are not of type Object while second will store references of Integer objects, which are of type Object.
arrayList1 is really a List of size one.
http://ideone.com/w0b1vY
arrayList1.size() = 1
arrayList3.size() = 7
The int[] is being cast to a single Object. That Object cannot be cast to Integer.

Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;

The map function simply iterates through the integer array and applies function to it and then adds it to an output array. I'm getting this error and I can't seem to find where it's casting an Object to an Integer. The map function returns an Integer array and is sent to printArray which takes an Integer array. Any ideas?
public static void main(String[] args)
{
Function<Integer,Integer> function = new CalculateSuccessor<Integer,Integer>();
Integer[] integerArray={1,3,4,2,5};
printArray(map(function, integerArray));
}
I've removed the rest of the code because the solution was found to be the <Integer, Integer> after Function.
its because you use generics Function<Integer,Integer> guava is trying to cast the values you pass as to Integer but you actualy pass Object.
I assume that your printArray method expects an Object[]
An Object[] is not an definition of a "super" instance of Integer[] even if Object is a super class of Integer.
Assume this code was valid:
Object[] array = new Integer[10];
then this would also be valid
array[0] = new Car("Mercedes");
But the latter should not be possible. Hence the "inheritance" restriction on arrays.
Same goes for list for example
List<Object> myList = new ArrayList<Integer>();
It will give you a compiler error.

Collection.toArray() method, detail about it

Why would not it work?
List<String> lista = new ArrayList<>();
lista.add("Lol");
lista.add("ball");
String [] array = (String[])lista.toArray();
It throws a RunTimeException (ClassCastException), I am aware that there is another method for the purpose of returning the object contained in the List, however what is happening behind the scenes? I mean I am casting an array of Objects which actually is an array of Strings to an Array of Strings. So it should compile, but it does not.
Thanks in advance.
That version of toArray() returns Object[]. You can't cast an Object array into a String array even if all the objects in it are Strings.
You can use the lista.toArray(new String[lista.size()]); version to get the actual type correctly.
List.toArray()
returns an Object[], because of type erasure. At runtime your list does not know if it has String objects. From there you can see where that error is coming from.
You cannot type cast an Object[] into a String[]
Array of objects is not array of Strings and can't be cast to one.
Check this.
use toArray(T[] a) instead.
Ie.
List<String> lista = new ArrayList<String>();
lista.add("Lol");
lista.add("ball");
String [] array = lista.toArray(new string[1]);
This insures that toArray returns an array of type String[]
As others have noted, toArray() returns an array of type Object[], and the cast from Object[] to String[] is illegal.
List lista = new ArrayList<>(); ---> List lista = new ArrayList();
There are two toArray() versions.You can use another one!

Java Array and ArrayList

When I try to create an ArrayList myArrayList from an array, using Arrays.asList(myArray), I am not getting the List of elements in myArray. Instead I get list of Array.
The size of myArrayList is 1 . When I try to do myArrayList.toArray(), I am getting a two dimensional array. What to do to get the elements of myArray in a list? Is iterating the only option??
Firstly, the asList method is the right method:
Integer[] myArray = new Integer[3];
List<Integer> myArrayList = Arrays.asList(myArray);
System.out.println(myArrayList.size()); // prints 3, as expected
The problem may be that you are calling the varargs asList method in such a way that java is interpreting your parameter as the first varargs value (and not as an array of values).
Object myArray = new Integer[3];
List<Object> myArrayList = Arrays.asList(myArray);
System.out.println(myArrayList.size()); // prints 1 - java invoked it as an array of Integer[]
To fix this problem, try casting your parameter as Object[] to force the varargs invocation, eg:
Object myArray = new Integer[3];
List<Object> myArrayList = Arrays.asList((Object[]) myArray); // Note cast here
System.out.println(myArrayList.size()); // prints 3, as desired
What is the type of myArray? You cannot use Arrays.asList with an array of primitive type (such as int[]). You need to use loop in that case.
There are different ways by which you can achieve it.3 example of converting array to arraylist and arraylist to array in java might help.
Would this work?
Object[] myArray = new Object[4]; //change this to whatever object you have
ArrayList<Object> list = new ArrayList<Object>();
for (Object thing : myArray) list.add(thing);
Try providing the generic type in the method call. The following gives me a list of 2 String elements.
String[] strings = new String[]{"1", "2"};
List<String> list = Arrays.<String>asList(strings);
System.out.println(list.size());

Categories