Error: Incompatible Types: List<Integer> Cannot Be Converted to Arraylist<Integer>

Incompatible types List of List and ArrayList of ArrayList

If you had a List<List<Integer>> then you'd be able to add a LinkedList<Integer> to it. But you can't do this for an ArrayList<ArrayList<Integer>>, so the latter can't possibly be a type of List<List<Integer>>.

incompatible types: List<ArrayList<Integer>> cannot be converted to List<List<Integer>>

See here for why you can't convert a List<ArrayList> to a List<List>.

To fix your error, on the other hand, you can simply not declare your variables as List<ArrayList<Integer>> and use List<List<Integer>> instead. You can do this because you are not using any members that only exists in ArrayList but not List. You also don't need to specify the generic arguments when instantiating a class, as they can be inferred.

public static List<List<Integer>> subsets(int[] nums) {
// notice the change here!
List<List<Integer>> subsets = new ArrayList<>();
if (nums.length ==1){
ArrayList<Integer> subset1 = new ArrayList<>();
ArrayList<Integer> subset2 = new ArrayList<>();
subset2.add(nums[0]);
subsets.add(subset1);
subsets.add(subset2);
return subsets;
}
int[] nums_1 = new int[nums.length-1];
for(int i = 0; i< nums.length-1; i++) {
nums_1[i]=nums[i];
}

// notice the change here!
List<List<Integer>> subsets2= subsets(nums_1);
// notice the change here!
ArrayList<List<Integer>> final_set = new ArrayList<>(subsets2);

// notice the change here!
for(List<Integer>subset: subsets2) {
ArrayList<Integer> subset1 = new ArrayList<>();
subset1.add(nums[nums.length-1]);
subset1.addAll(subset);
final_set.add(subset1);
}
return final_set;
}

error: incompatible types: List<Integer> cannot be converted to ArrayList<Integer>

Correct way to iterate your list of list should be:

    for(List<Integer> subset:allsubsets) { 

instead of:

    for(ArrayList<Integer> subset:allsubsets) { 

List<List<Integer>> allsubsets is declared as List of List, but the implementation is unknown.

Only you know the type of nested List is ArrayList, so either change foreach to use List<Integer> or manually cast your List<Integer> to ArrayList<> (this is not preferred)

One more thing:

    allsubsets.add(moresubsets); // add to actual one

This try to add a List of List (List<List<Integer>>) as element which should be List<Integer> hence compile error.

Change that statement to:

  allsubsets.addAll(moresubsets);

List<Integer> cannot be converted to ArrayList<Integer>

Think of it like this, you have a Fruit called re (I use this name because it's the name of the variable you are using).

Fruit re;

You have a method reverse whose input type is Apple.

public Apple reverse(Apple a) {
// ...
}

We have a variable re that we declared as Fruit which means we're saying it's always going to be some kind of Fruit, perhaps Apple, but maybe Orange -- or even Banana.

When you try to give the Fruit to the method taking Apple the compiler stops you, because it can't be certain that it's 100% an Apple. For example...

Fruit re = new Orange();
reverse(re);

Yikes! We are putting a square peg into a round hole so to speak. reverse takes Apple, not Orange. Bad things could happen!

Side note: Why is it okay then to assign Apple to something declared as Fruit then? (reverse returns an Apple, Fruit f = reverse(re); is legal.) Because an Apple is a Fruit. If it were declared as the more specific Apple and the return type were the more general Fruit, then there would be an issue here. (If reverse returned Fruit, Apple a = reverse(re); would be illegal.)

If you didn't follow the metaphor, replace Fruit with List and Apple with ArrayList and read the above again. List is Fruit, a general way to describe an abstract idea. ArrayList is Apple, a specific implementation of the abstract idea. (LinkedList could be Orange too.)

In general you want to declare things as the most general thing you can to get the functionality you need. Making the below change should fix your problem.

public List<Integer> reverse(List<Integer> list) {

We are taking some kind of List of Integers and returning some kind of List of Integers.

Cannot convert ArrayList<ArrayList<Integer>> to ArrayList<ArrayList<Object>>

There are 2 solutions:

1.Use generic (and btw should use List instead of ArrayList)

public static <T> List<List<T>> foo(List<List<T>> parameter) {
//do something
}

public void test() {
List<List<Integer>> parameter;
//do something with the parameter
List<List<Integer>> product = foo(parameter);
}

2.Use nested wildcards:

public static List<? extends List<? extends Object>> foo(List<? extends List<? extends Object>> parameter) {
//do something
}

public void test() {
List<List<Integer>> parameter = new ArrayList<>();
//do something with the parameter
foo(parameter);
}

Not able to understand how to define a List of List in java

You could use the diamond operator, as GhostCat suggested, and let the compiler worry about the correct type.

But if you want to understand what the correct type should be, use:

List<List<Integer>> arr2 = new ArrayList<List<Integer>>(); 

You are instantiating a List of something (let's forget for a second that something happens to be a List<Integer>), so you need to create an instance of a class that implements the List interface - ArrayList in this case. So you create an instance of ArrayList of something.

The element type (my so called "something") - List<Integer> in your example - remains unchanged.

Now, when you want to add an element to your List, you need to create an instance of a class that implements List<Integer>:

List<Integer> inner = new ArrayList<Integer>();
arr2.add(inner);

Reading CSV file into an array in Java [incompatible types: Integer cannot be converted to int[].]

1. Dealing with this error: Integer cannot be converted to int[]

We can only assign int[] to an int[], lines is a List of Integer and so when we try to get any element in lines it will always return an Integer.

So int thirdCountry[] = lines.get(3); basically means int[] = some int value and that's what causes the above issue. So inorder to fix it I declared lines like this ArrayList<String[]> lines.

2. Now, why the List is of String[] type?

Since the data can be a String, int, or a double, it is safe to have a String[] which would accept all three.

3. Blind rule while getting any element from an Array or Collection

Whenever you are trying to get some element from an array or collection, always check the length or size and that index should be less than the length of the same.

public static void main(String[] args) throws IOException {
System.out.println("Data from CSV file to be analysed:"+"\n");
String file = "jrc-covid-19-all-days-of-world_ASSIGNMENT-FIXED.csv";
ArrayList<String[]> lines = new ArrayList<String[]>();
String line = null;

try(BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
int i = 0;
while(((line = bufferedReader.readLine()) != null) && i<27) {
lines.add(line.split(","));
System.out.println(Arrays.toString(lines.get(i)));
i++;
}
}
catch (IOException e) {
e.printStackTrace();
}

if(lines.size() > 3) {
String thirdCountry[] = lines.get(3);

if(thirdCountry.length > 6) {
String cp3 = thirdCountry[6];
System.out.println(cp3);
}
}

}

4. Adding numbers

For adding we need to convert the String values to numeric values (int, long, or double). Let's say we are converting to int, so the sample values can be "123", "abc", "abc123", or "" (an empty string). So you can try like this

String s1 = "";
int total = 0;
try {
total += Integer.parseInt(s1);
} catch (NumberFormatException e) {
System.out.println("Not a number!");
}
System.out.println(total);

You can modify this for long and double as per your comfort.



Related Topics



Leave a reply



Submit