
I am using Array list in Java but when i compile i get array list type error. Can anyone please explain what is array list type error and why it occurs?

Type error occurs due to mismatch of data type. In this example, Arraylist is of Integer type - ArrayList<Integer> Data Apple and Cherry are String type. So When you try to add String data into Integer type ArrayList, it will generate error. In the same way when you try to add integer data into String type ArrayList ,error occurs. Whenever you declare Arraylist type beware in the time of adding data.

import java.util.*;
public class ArraylistTypeError {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add("Apple");
list.add("Cherry");
System.out.println(list);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.