Typo boy
Typo boy

Reputation: 85

ArrayList no suitable method

I want to add everything from ArrayList in class Taster in another ArrayList in class Tastatura. It's calling no suitable method error and I don't really know what to do and how to do it . I would be really thankfull if someone quickly explained me how this work and write my code correctly.

This is main class:

public class Tastatura {     
public static void main(String[] args) {
Taster slovo = new Taster();

ArrayList<String> collection1 = new ArrayList<String>();
collection1.addAll(slovo);  //<--- error here
 }
 }

another class:

public class Taster {
    public Taster(){
     ArrayList<String> Slova = new ArrayList<String>();
         Slova.add("q");
         Slova.add("w"); }}

Upvotes: 0

Views: 100

Answers (3)

xxxvodnikxxx
xxxvodnikxxx

Reputation: 1277

You are trying to add class Taster into method, which expects String or something like that (ArrayList<String> in this case)

:)

Nafas code should work..

Other way is to write your own "add" method in your Taster class (which takes actual ArrayList and adds in it every item from ArrayList given in param, and returns that result)

Upvotes: 0

Soumitri Pattnaik
Soumitri Pattnaik

Reputation: 3556

The problem is by collection1.addAll(slovo); you are adding an object to a collection.

The addAll method requires a Collection as argument.

You class should look like this :

public class Taster {
    private ArrayList<String> Slova;

    public Taster() {
        Slova = new ArrayList<String>();
         Slova.add("q");
         Slova.add("w"); 
    }

    public ArrayList<String> getList() {
        return Slova;
    }
 }

And in you main method :

collection1.addAll(slovo.getList());

OR

You don't need to change the Taster class :

Just change your addAll to :

collection1.addAll(slovo.Slova);

Upvotes: 1

nafas
nafas

Reputation: 5423

try this :

collection1.addAll(slovo.Slova) ;

and change your Taster class to this :

public class Taster {
    public ArrayList<String> Slova = new ArrayList<String>();
    public Taster(){
         Slova.add("q");
         Slova.add("w"); 
   }
}

Upvotes: 0

Related Questions