0

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"); }}
3
  • 4
    Well a Taster isn't a collection of strings - so it doesn't make sense to call collection1.addAll(slovo)... Do you at least understand the error? Commented May 26, 2015 at 9:04
  • Please don't make the mistake of having your class extend ArrayList. This is not an inheritance situation. Commented May 26, 2015 at 9:05
  • @JonSkeet , yeah , I'm a bit slow minded , I got it now . Thank you for your answer. Commented May 26, 2015 at 9:12

3 Answers 3

1

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);

Sign up to request clarification or add additional context in comments.

3 Comments

You and the OP should realize that private means nothing when you write code like this. Anyone who calls that getList() method gets the private reference and can alter the state of that class as they wish.
@duffymo edited the answer, and added the simplest solution as "or". Thanks :)
doesn't change anything. That getList() method still renders your private modifier meaningless.
0

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"); 
   }
}

1 Comment

Now I noticed what I did wrong , this is working now . thank you. +1
0

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)

1 Comment

Yeah , it worked , I noticed what I did wrong when he corrected it . Thank for your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.