0

I'm trying to add string objects to a linked list but i keep getting an error when I try to return the list

import java.util.LinkedList;
import java.util.ListIterator;

public class StacksAndQueues {
    LinkedList<String> list = new LinkedList<String>();
    private String[] words = {"goats", "cheese"};

    public StacksAndQueues() {}

    public String Add() {
        /**
           This method is used to load an array of string objects
           into a linked list
        */
        int x;
        for (x=0; x < words.length; x++) {
            list.add(x, words[x]);
        }

        return list;
    }
}
0

2 Answers 2

3

If you want to return the list from your Add() method then you will need to change the signature of the method.

public LinkedList<String> Add () {
    /**
     This method is used to load an array of string objects
     into a linked list
    */
    int x;
    for (x=0; x < words.length; x++) {
        list.add(x, words[x]);
    }

    return list;
}
Sign up to request clarification or add additional context in comments.

Comments

0

in your code you are trying to return list while return type is given as String, change return type to List and it will work.

Comments

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.