0

This is the top portion of my ArrayListStack class. I am trying to make values become an array list of T of 10. I am getting an error "incompatible types required: ArrayListStack found:java.util.ArrayList". In the line values = new ArrayList(10)". How would i properly set this value thanks

 import java.util.ArrayList;

public class ArrayListStack<T> implements StackInterface<T> 
{
    private ArrayListStack<Integer> values= new ArrayListStack<Integer>();
    private int size;

    public ArrayListStack()
    {
        size = 0;
        values = new ArrayList<T>(10); 

    }
1
  • Have you checked the error? values has even different types as a field and in the constructor. Commented Feb 13, 2014 at 20:38

5 Answers 5

2

I think you are attempting to use an ArrayList to back your ArrayListStack. It makes no sense for an ArrayListStack to contain another ArrayListStack. If this were to compile, each new ArrayListStack would create another ArrayListStack, which would create another ArrayListStack, etc.

Change the type of your values to ArrayList, and use your T generic type parameter:

private ArrayList<T> values;

No need to initialize it here; you are initializing it in the constructor.

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

3 Comments

I think after looking at his code that he meant to extend ArrayList
@StormeHawke It's possible to do that; it's one way of creating an ArrayListStack. But since the constructor is creating its own ArrayList, I think the OP is going for composition over inheritance.
@StormeHawke If i am trying to push something into it, this gives me an error, but isnt it correct for array lists?? public void push(T value) {value = values.add(size); size++;}
0

There are multiple errors here:

  • you declare values as an ArrayListStack while it should be an ArrayList
  • you declare values by specifying the type parameter to Integer while it should stay T
  • you initialize values outside the constructor to inizialize it again inside the constructor
  • you try to initialize values to a different type, with a different type parameter

It should be

private ArrayList<T> values;

ArrayStackList() {
  value = new ArrayList<T>(10);
}

Comments

0

You are assigning a generic type to an array list of Integer objects. Furthermore you are already initializing the list in your class definition.

Comments

0
public class ArrayListStack<T> implements StackInterface<T> 
{
    private ArrayList<T> values; // <-- Note change here
    private int size;

    public ArrayListStack()
    {
        size = 0;
        values = new ArrayList<T>(10); 
    }
}

You need to have the same type and generic type for values when you construct it and when you declare it. T is your generic type (a generic generic type, if you will). I also removed the superfluous initialization at the field declaration, since you're re-constructing it in the constructor.

2 Comments

But if i try to add something to the array list in a push function, i get an error. public void push(T value) {value = values.add(size); size++;}. Is there a special way i have to add something??
@user3071909, ArrayList.add returns a boolean, not T, and accepts T, not int. You should use public void push(T value) { values.add(value); size++; }
0

If you're trying to make a class that combines the functionality of ArrayList and StackInterface, make the following changes. Otherwise your code doesn't seem to make much sense

import java.util.ArrayList;

public class ArrayListStack<T> extends ArrayList<T> implements StackInterface<T> 
{
    public ArrayListStack()
    {
        //You can set this to whatever initial size you like, 
        //however if you look at the source code of ArrayList
        //you'll discover that 10 is the default size of an ArrayList
        super(10); 
    }
    //Add whatever methods are required by StackInterface
}

In this situation, you can seamlessly use your class as both a standard ArrayList and a StackInterface

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.