difference between List<Integer> arr and var arr - java

The following code works fine.
List<Integer> arr = new ArrayList<Integer>();
System.out.println(arr.getClass().getSimpleName()); // output: ArrayList
arr.add(0);
arr.add(1);
arr = arr.subList(0,1);
But if you change the first line to
var arr = new ArrayList<Integer>();
an error will occur:
java: incompatible types: java.util.List<java.lang.Integer> cannot be converted to java.util.ArrayList<java.lang.Integer>
However, even if arr is defined in the second way, its type is still ArrayList, so what is the difference?

When you write
var arr = new ArrayList<Integer>();
the compiler infers the type of the arr variable to be ArrayList<Integer>, so it's equivalent to writing:
ArrayList<Integer> arr = new ArrayList<Integer>();
arr.subList(0,1) returns a List<Integer>, which cannot be assigned to an ArrayList<Integer>, since an ArrayList<Integer> is an implementation of List<Integer>, but not all implementations of List<Integer> are ArrayList<Integer>.
On the other hand, when you declare the variable as List<Integer> arr, you can assign to it any implementation of List<Integer>, which includes the value returned by arr.subList(0,1).
It's important to note that subList does not return an ArrayList.
For example, subList() method of ArrayList returns an instance of an inner class called ArrayList$SubList. Such List cannot be assigned to a variable whose type is ArrayList.

Related

ArrayList's toArray() Method not Working Properly

When looking through ArrayList's methods, I saw a method called toArray(). I tried out this method using the following code:
ArrayList<Integer> a = new ArrayList<>();
// Assigning random elements to the ArrayList
int[] b = a.toArray();
However, this showed the following exception in the compiler:
Incompatible types.
Required: int[]
Found: java.lang.Object[]
The next thing I tried next is down-casting it to int[]
ArrayList<Integer> a = new ArrayList<>();
// Assigning random elements to the ArrayList
int[] b = (int[]) a.toArray();
This showed another error:
Cannot cast java.lang.Object[] to int[]
The last thing I tried is making it an Integer[] instead, and down-casting it to Integer[]
ArrayList<Integer> a = new ArrayList<>();
// Assigning random elements to the ArrayList
Integer[] b = (Integer[]) a.toArray();
This one compiled, but as soon as I ran it it produced ClassCastException. How do I use this toArray() method without errors?
List can only hold reference types (like Integer). Integer is a wrapper type. To convert a List<Integer> to an int[] there is an option using a Stream to map the Integer values to int and then collect to an int[]. Like,
int[] b = a.stream().mapToInt(Integer::intValue).toArray();
System.out.println(Arrays.toString(b));
It's all written in the javadoc:
Integer[] b = a.toArray(new Integer[0]);
Extending the answer of #Matthieu, It seems you need to pass new Integer[]. Attaching an example link given in geeksforgeeks.
Integer[] arr = new Integer[a.size()];
arr = a.toArray(arr);

Defining an array of List of integers in Java

I have an array. For each element of the array, I want to store multiple integers. I know that in C I can make an array of integer pointers, and use that pointer to make a list.
In Java, I can make an array of object 'A', where A has a list of integers. But why can't I do something like this
List<Integer>[] arr = new ArrayList<Integer>[]();
I get:
Type mismatch: cannot convert from ArrayList to List[]
You typically want to avoid mixing collections and arrays together for this very reason; an array is covariant (that is, Integer[] is-an Object[]), but collections (and generics in general) are invariant (that is, a List<Integer> is not a List<Object>).
You can definitely create a list of lists instead, which will ensure type safety and get you around the issue of creating a generic array:
List<List<Integer>> intNestedList = new ArrayList<>();
As stated in Java's own documentation, you cannot create an array of generics.
If you want to create an array which can hold up to ten List<Integer> you must declare the array that way.
List<Integer>[] arr = new ArrayList[10];
following assignment is valid
List<Integer> intList = new ArrayList<>();
arr[0] = intList;
whereas following will fail with an compilation error
List<String> stringList = new ArrayList<>();
arr[0] = stringList;
the compilation fails with
incompatible types: java.util.List<java.lang.String>
cannot be converted to java.util.List<java.lang.Integer>
An ArrayList is a List, but an ArrayList is not a List[]
If you want an Array of Lists that hold integers, I would suggest:
List<Integer>[] xyz; // still writing code will update in a sec
It turns out you can't create arrays of parameterized types, according to the oracle docs.
Unless you know for sure you want a finite array, I suggest you do something like List<List<Integer>> arr = new ArrayList<List<Integer>>();
If you really want an array of Lists then you'll want to see this Java question about ArrayList<Integer>[] x
Creating an array of List is no different than creating an array of any other object. You can do any of the following:
List[] listsArray = new List[3];
listsArray[0] = new ArrayList();
listsArray[1] = new LinkedList();
listsArray[2] = new ArrayList();
Or:
List[] listsArray = new List[]{new ArrayList(), new LinkedList(), new ArrayList()};
Note that you are limited in what you can do with generics on arrays.
Not a very nice solution but you might try it with a cast. Something like this:
List<Integer>[] arr = (List<Integer>[]) new List[SIZE_OF_YOUR_ARRAY];
You will probably get a warning but it should still work.
As i found, you need an array of arrays.
you can do this, to make your inner arrays:
Integer[] array1 = new Integer[];
Integer[] array2 = new Integer[];
and then put them in another array like this:
Integer[][] arrays = new Integer[][] { array1, array2 };
or
Integer[][] arrays = { array1, array2 };
maybe it's better to do it like this:
List<List<Integer>> listOfLists = Lists.newArrayList();
listOfLists.add(Lists.newArrayList("123","456","789"));
after all I recommend you to read this:
How to make an array of arrays in Java
Graph Implementation using Adjacency List depicts the usage of an Array of List.
public class Graph {
int vertex;
LinkedList<Integer> list[];
public Graph(int vertex) {
this.vertex = vertex;
list = new LinkedList[vertex];
for (int i = 0; i <vertex ; i++) {
list[i] = new LinkedList<>();
}
}
}
As you can observe that the constructor of class Graph, is used to define the Array of List.
in the same constructor, Array Of List is initialized too.
Hope It would be Helpful to resolve your problem and requirement !.

Can we have an Array of a collection?

I was trying create an array of a collection as follows.
ArrayList<Integer> ar[]=new ArrayList<Integer>[50];
but it gives me an error -> generic array creation
can anybody explain me why is it?
You can't create arrays of generic types. Use collection of collections instead:
ArrayList<ArrayList<Integer>> = new ArrayList<ArrayList<Integer>>();
Why can't we create an array of generic type? Array stores they exact type internally, but due to the type erasure at runtime there will be no generic type. So, to prevent you from been fooled by this (see example below) you can't create an array of generic type:
//THIS CODE WILL NOT COMPILE
ArrayList<Integer>[] arr = new ArrayList<Integer>[5];
Object[] obj = arr;
obj[0] = new ArrayList<Long>(); //no one is safe
ArrayList is internally a 1D array itself. what you need 2D array so you can create
ArrayList<Integer[]> ar=new ArrayList<Integer[]>();
or
ArrayList<ArrayList<Integer>> = new ArrayList<ArrayList<Integer>>();
The answer to you question can be found in the Java Language Specification. You are trying to create an array using Array Creation Expression. It says the following: "It is a compile-time error if the ClassOrInterfaceType does not denote a reifiable type". Because arrays in Java are created at runtime, their type information should be completely available at runtime. In other words array element type should be a reifiable type. Generics in Java are implemented using type erasure (i.e. only subset of generics compile-time type information is available at runtime) hence they are not reifiable by definition and therefore you cannot create arrays of generic types.
Actually you can have an Array of Collection, it just is not allowed that the Collection has a specific type.
You can do something like
ArrayList<?>[] ar = new ArrayList<?>[50];
// or ArrayList[] ar = new ArrayList[50];
ar[0] = new ArrayList<Integer>();
but you will not have the benefits of Generics - there is no type information for the content of the Collection, you will need to cast when reading from it
Integer i = (Integer) ar[0].get(0);
You could do something like this
ArrayList<Integer> ar[]= new ArrayList[50];
ArrayList<Integer> intArr = new ArrayList<Integer>();
ArrayList<Long> longArr = new ArrayList<Long>();
ar[0]=intArr;
ar[1]= longArr; // compile error Type mismatch: cannot convert from ArrayList<Long> to ArrayList<Integer>
You can have an array "technically" of type ArrayList but its a bit nit picky. Create it as an ArrayList<ArrayList<Integer>> list = ArrayList<ArrayList<Integer>(); and convert it using toArray(ArrayList<Integer>[list.size()]);.
Example:
ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
list.add(new ArrayList<Integer>());
list.add(new ArrayList<Integer>());
list.add(new ArrayList<Integer>());
int count = 1;
for(ArrayList<Integer> AList: list) {
for(int i = 0; i < 4; i++)
AList.add(count++);
}
ArrayList<Integer>[] check = list.toArray(new ArrayList[list.size()]);
for(ArrayList<Integer> AL : check) {
for(Integer i:AL) {
System.out.print(i + " ");
}
System.out.println();
}
Output:
1 2 3 4
5 6 7 8
9 10 11 12
Works and is an ArrayList array

Downcasting from Object to Integer Runtime Error: java.lang.ClassCastException

Run time exception-- java.lang.ClassCastingException...
Integer intArr[] = new Integer[arrList.size()];
ArrayList <Integer> arrList =new ArrayList();
intArr=(Integer[])arrList.toArray(); // returns Object class which is downcaste to Integer;
I understand down-casting is not safe but why is this happening?
I also tried to converting ArrayList to String to Integer to int, but I get the same error.
Try to do this
intArr = arrList.toArray(new Integer[arrList.size()]);
What you get is a typed Integer Array and not a Object array.
First of all, this doesn't bind the ArrayList to type Integer.
ArrayList <Integer> arrList =new ArrayList();
Instead, this is what happens, arrList is assigned to an ArrayList of raw type, but that isn't a problem.
The problem lies in,
intArr=(Integer[])arrList.toArray();
since arrList is a raw-type (due to the assignment, it gets assigned as new ArrayList<Object>() by the compiler), you're effectively getting an Object[] instead.
Try assigning arrList to new ArrayList<Integer>() and do this:
intArr = arrList.toArray(new Integer[arrList.size()]);
The problem here is that you are trying to convert an array of objects to an array of integers. Array is an object in itself and Integer[] is not a sub-class of ArrayList, nor vice versa. What you have to do in your case is cast individual items, something like this:
Integer intArr[] = new Integer[arrList.size()];
for(int i=0; i<intArr.length; i++)
{
intArr[i] = (Integer)arrList.get(i);
}
Naturally, you may get ClassCastException if individual elements in the array list are not of type Integer.
toArray(T[] a) takes a paramter:
"a - the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runt"

Why does Arrays.asList on an int[] return a List<int[]>, and not a List<int>?

Consider this code:
int[] tcc = {1,2,3};
ArrayList<Integer> tc = Arrays.asList(tcc);
For the above, Java complains that it cannot convert from List<int[]> to ArrayList<Integer>.
What's wrong with this?
Why is it List<int[]> and not List<int>?
An ArrayList can hold only objects not primitives such as ints, and since int != Integer you can't do what you're trying to do with an array of primitives, simple as that. This will work for an array of Integer though.
This will work:
ArrayList tc = new ArrayList(Arrays.asList(1,2,3));
You could have it as:
List<int[]> tc = Arrays.asList(tcc);
Arrays.asList returns a List, not an ArrayList. And since Arrays.asList is a varargs function, it thinks tcc is one element of a bigger array.
If you want to have just a List of Integers, you'd have to rewrite it as SB mentioned in the comments for Hovercraft Of Eel's answer:
List<Integer> tc = Arrays.asList(1, 2, 3);
Alternatively, if you make tcc an Integer[], you can still use your array as an argument in the following snippet by explicitly asking for a List of Integer, providing a type parameter that agrees with the passed array:
Integer[] tcc = {1,2,3};
List<Integer> tc = Arrays.<Integer>asList(tcc);

Categories

Resources