0

I've got a method which goes like this -

public IntSequence subSequence(int index, int size) {
//IntSequence is an interface and currently this thing is inside a class that's
//implementing it

    ArrayList<Integer> valuelist = new ArrayList<>();
    for(int i = a1; i <= a2 + a1; i++)
    {
        if((a1 + a2) <= a.length)
        valuelist.add(a[i]);
    }
    return valuelist;
}

My problem here is, I just want to return a sequence of integers however what I am returning here is an ArrayList and the compiler says cannot convert from type IntSequence to ArrayList.

(I'm not allowed to change the parameters of the method)

Thanks for acknowledging the problem!

EDIT :

This is my IntSequence interface -

public interface IntSequence {

   int length();

   int get(int index);

   void set(int index, int value);

   IntSequence subSequence(int index, int size);
}
3
  • 3
    It seems like you would need to build an instance of IntSequence, well, a class that implements it, right? Commented Mar 5, 2014 at 23:42
  • Where is your IntSequence defined? Can you show the code? Commented Mar 5, 2014 at 23:44
  • You may be able to define a class that implements IntSequence and defines a constructor with an ArrayList<Integer> parameter. But it's hard to give you any more help without seeing what IntSequence looks like. Commented Mar 5, 2014 at 23:44

3 Answers 3

1

Without seeing IntSequence, it's hard to give a specific answer. But you probably want to do something like:

class ArrayIntSequence implements IntSequence {

    private ArrayList<Integer> arr;

    public ArrayIntSequence (ArrayList<Integer> arr) {
        this.arr = arr;
    }

    public ... 
    // provide bodies for all the methods defined in IntSequence, implemented
    // using "arr"

}

and then your return statement in subSequence becomes

return new ArrayIntSequence(valuelist);

EDIT: now that you've included the definition of IntSequence, the implementation of length, get, and set are very simple using the similar ArrayList methods, and it looks like you've already got subSequence except that you can tweak it to use an ArrayList instead of an array.

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

Comments

1

Try this:

public IntSequence subSequence(int index, int size) {
    // ...
    final ArrayList<Integer> valuelist = new ArrayList<>();
    // ...
    return new IntSequence() {
       // Compiler will tell you what to put here
    };
 }

The compiler will give you some errors, telling you what methods you need to implement in order to return an IntSequence. If this is too much you might want to create a new object of the class that implements the interface and see if you can pass just the right contents to a constructor.

Comments

0

Your method implements a method that returns IntSequence. If you try to return an ArrayList<Integer> instead, you are not actually fulfilling the interface. You will need to convert the ArrayList to whatever IntSequence is and return that.

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.