arraylist item cannot be applied to another list

adding elements to arraylist in java using for loop
java list arraylist
arraylist add
arraylist remove
arraylist length
initialize arraylist java
arraylist get
whats an array list

i have a problem with array list. i have the following

 private ArrayList<?> selectedTransactionList;
  private ArrayList<?> transactionList;

then i am initializing like this:

 selectedTransactionList = new ArrayList<Income>();
 transactionList = new ArrayList<Income>();

with Income been a custom class with getter and setters function. after populating the transactionList with data, i am doing this: selectedTransactionList.add((Income)transactionList.get(1));

however, android is giving error such as: add (capture) in ArrayList cannot be applied to (com.erod.finsec.Income)

iam declaring the array as ArrayList because i have other classes that i need to create array list. for example:

  selectedTransactionList = new ArrayList<Expense>();
     transactionList = new ArrayList<Expense>(); 

same code logic applies for expense and income and others and i dont want to create separate class with same code but only difference is either array list is income,expense , other type. how can i solve erro above ?

ArrayList<?> doesn't allow to add elements to it as you could read on I suggest to change type <?> to <T> and then use generic class for that

java - arraylist item cannot be applied to another list, however, android is giving error such as: add (capture) in ArrayList cannot be applied to (com.erod.finsec.Income). iam declaring the array as  You are using POJO class not string so do the required changes in adapter as well as in your list type. use this. ArrayList<PasswordItem> tasklist = new ArrayList<>(); instead of . ArrayList<String> tasklist = new ArrayList<>(); plus mAdapter should be of CustomAdapter type so . declare it like CustomAdapter mAdapter and initialize it later as

Simply specify ArrayList type while declaring:

private ArrayList<Income> selectedTransactionList;
private ArrayList<Income> transactionList;

Java 7 Recipes: A Problem-Solution Approach, synchronizedList(new ArrayList<String>()); startUpdatingThread(list); efficient (​newer) concurrent collections like the ConcurrentMap cannot be used in this a List object (it can be ArrayList, LinkedList, or another List implementor) into a List​  It is not the best because the size of the list returned from asList() is fixed. We know ArrayList is essentially implemented as an array, and the list returned from asList() is a fixed-size list

I would suggest you to create base abstract class/interface and extend/implement this in your Income and Expense class.

You can create an interface like below

interface Item {
  // add common methods if required

Then simply implement this interface

class Income implements Item {
  // Keep rest of the code as it is
class Expense implements Item {
  // Keep rest of the code as it is

Then declare your list of type Item.

private List<Item> selectedTransactionList;
private List<Item> transactionList;

Learning Java, It is not some new kind of List that can hold either String or Date elements. It does not create a new type of collection that can contain either Foos or Bars. an object, but they cannot be used as the type to create an instance of an object. new ArrayList<Date>(); listOfAssignableFromMyDate = new ArrayList<Object>(​);  What is an Integer? And why cannot we use int in an ArrayList? An Integer is a reference type (a class). An int is a value. And: The ArrayList requires, in its implementation, a reference type. So int is not allowed. Quote: The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int (Java Documentation).

It looks as if you want a class specifically for the common abstraction.

class Transactions<T> {
    private final List<?> selectedTransactionList = new ArrayList<T>();
    private final List<?> transactionList = new ArrayList<T>();

(Note, this is a case when the plural is correct.)

The code dealing specifically with Income uses it as:

Transaction<Income> transactions = new Transaction();

Likewise Expense.

Transaction<Expense> transactions = new Transaction();

If there is a common interface between Income and Expense add that as a bound to the Transaction class type parameter. This allows methods within Transaction to use methods within the common type.

class Transactions<T extends RelevantAccountancyTerm> {

Top 10 Mistakes Java Developers Make, This list summarizes the top 10 mistakes that Java developers frequently make. #​1. Convert Array to ArrayList To convert an array to an ArrayList, In the foreach loop, compiler will make the .next() called after the operation of removing element​, So very often you don't need Hashtable , instead HashMap should be used. If the passed array doesn’t have enough space, a new array is created with same type and size of given list. If the passed array has more space, the array is first filled with list elements, then null values are filled. It throws ArrayStoreException if the runtime type of a is not a supertype of the runtime type of every element in this list.

java for complete beginners - array lists, Once you have a new ArrayList objects, you can add elements to it with the add method: The next method can be used to go through all the items in the list. Yes, it is true that a list of strings is also a list of objects, but you cannot add an object to a list of string in such type-safe language like Java. Java, with good intention, protects you from doing that.

Java.util.ArrayList.add() Method in Java, Below are the add() methods of ArrayList in Java: boolean add(Object o) : This method appends the specified element to the end of this list. Parameters: object o​:  Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.)

ArrayList (Java Platform SE 7 ), The capacity is the size of the array used to store the elements in the list. Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to (In other words, this method must allocate a new array)​. method burnGas in class GasTank cannot be applied to given types; required: double, double found: no arguments reason: actual and formal argument lists differ in length The operator that you use here cannot be used for the type of value that you are using it for. You are either using the wrong type here, or the wrong operator.