1

Is it possible to create and use an ArrayList within a method? I would like to create a temporary stack in the public int min() method to track the smallest value. However, the compiler complains about the ArrayList add() method: Cannot resolve method 'add(int)' I presume that's because I'm trying to run it inside the same method that created the ArrayList?

import java.util.*;

public class MinStack {
private ArrayList<Integer> data;


public MinStack() {
    data = new ArrayList<Integer>();
}

public void push( int a ) {
    data.add( a );
}

public int pop() {
    if ( data.size() <= 0 ) {
        throw new IllegalStateException();
    }
    return data.remove( data.size() - 1 );
}

public int size() {
    return data.size();
}

public int min() {
    MinStack smalls = new MinStack();
    int elMin = (data.get((data.size() - 1)));
    smalls.add(elMin);
    while (!data.isEmpty()) {
        if (data.get(data.size() - 1) < elMin)
        elMin = data.get(data.size() - 1);
    }
    return elMin;
}
3
  • push != add. Commented Aug 29, 2018 at 4:29
  • smalls.add(elMin); --> smalls.push(elMin); Commented Aug 29, 2018 at 4:30
  • Why do you think there could be a restriction about using an ArrayList, or anything at all, “within a method”? Besides that, if all you want to use, is pushing and popping, you may use an ArrayDeque instead. Commented Aug 30, 2018 at 11:16

5 Answers 5

3

You are not calling add method on an instance of Arraylist but instead you are calling it on an instance of MinStack class object which doesn't have add method.

You should be calling push method instead as it is defined in your class which will call add method of the arraylist. Or else define/rename(push) a method in MinStack class with name add

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

Comments

2

You have used a improper design strategy to implement this class. Their are lots of flows in your implementation and here i am not gonna talk about those. Within you design approach, Instead of calling implemented behavior call push() in your class. You are trying to call a method which is not implement in this class called add(). If you want to use your implented behaviour within the class, Just call your method as smalls.push(elMin) instated of smalls.add(elMin).

Comments

1
smalls.add(elMin)

is looking for add method of MinStack class. You should call push method to insert elMin in data arrayList.

Comments

1

i think you can do this like below:

public int min() {

    if(data.isEmpty())
        throw new RuntimeException("the list is empty...");
    int elMin = data.get(0);
    for(int i : data) {
        if(i < elMin) {
            elMin = i;
        }
    }
    return elMin;
}

Comments

0

Try this :

Call push method to insert elMin in ArrayList data.

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.