Home > Blockchain >  Could you help me with this error "The method in undefined for the type"?
Could you help me with this error "The method in undefined for the type"?

Time:06-05

I was trying to populate an Arraylist using asList() but I I cant understand the error Eclipse is giving me, I read other solutions but I dont know to use them for my case

package arraylistvirtuale;
import java.util.ArrayList;
import java.util.Arrays;
public class BankAccountArraylisttester {

    public static void main(String[] args) {
        
        BankAccount cc = new BankAccount(1115);

        ArrayList<BankAccount> accounts 
        = new ArrayList<BankAccount>(Arrays.asList(cc(1115)));
        
        accounts.add(1, new BankAccount(1008));
        accounts.remove(0);
        System.out.println("size="   accounts.size());
        BankAccount first = accounts.get(0);
        System.out.println("first account number="   first.getAccountNumber());
        BankAccount last = accounts.get(accounts.size()-1);
        System.out.println("last account number="   last.getAccountNumber());
        BankAccount bankaccount= new BankAccount(2385);
        bankaccount.deposit(100.1); 
        System.out.println("balance ="   bankaccount.getBalance());
    }

}

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method cc(int) is undefined for the type BankAccountArraylisttester

    at arraylistvirtuale/arraylistvirtuale.BankAccountArraylisttester.main(BankAccountArraylisttester.java:11)

CodePudding user response:

It is unclear what you intend with Arrays.asList(cc(1115)) -- in Java source, the syntax x(y) is an invocation of x, passing y as a parameter. So the error message is telling you exactly what is wrong -- there is no method named cc with an int as a parameter in the BankAccountArraylisttester class.

The Arrays.asList() method takes an array as a parameter; you have passed it what looks like an individual object, if we ignore the (1115). So even if you remove the (1115), you will have an error indicating that there is no asList() method taking a BankAccountArraylisttester as a parameter.

Perhaps you wanted to put cc into an ArrayList<BankAccount>; if so, you can create the ArrayList, then add cc to it:

ArrayList<BankAccount> accounts = new ArrayList<>();
accounts.add(cc);

I've speculated enough without trying to figure out what first and last are supposed to be.

CodePudding user response:

If you have already assigned a value to a variable, then use this variable. The variable already contains what you have written to it.

    BankAccount cc = new BankAccount(1115);
    BankAccount dd = new BankAccount(1234);
    BankAccount ee = new BankAccount(3333);
    ArrayList<BankAccount> accounts
            = new ArrayList<BankAccount>(Arrays.asList(cc, dd, ee));

Or:

   ArrayList<BankAccount> accounts
        = new ArrayList<BankAccount>(Arrays.asList(
   new BankAccount(1234);
   new BankAccount(2222);
   new BankAccount(3333);
   ));

asList() method fills in either list you have created or another list that has already been created.

For example:

 ArrayList<BankAccount> accounts
 = new ArrayList<BankAccount> 
 (Arrays.asList(name_of_another_list));
  • Related