我可以在自己的类中声明一个对象吗?

时间:2017-03-24 02:27:47

标签: java data-structures

我正在学习数据结构。我已经尝试做一个练习,用随机数命令两个堆栈,始终按照从最小到最大的顺序通过比较并将值传递给另一个名为aux的堆栈。我有一个方法按顺序填充它们但我在新类中创建对象aux时遇到问题。我遇到了问题<init>。我不是那个意思。

PD:在堆栈顺序完成后,我将它们传递给一个总是必须按从最大到最小的顺序排队。

class Pilas {

    private int max;
    private int[] pila;
    private int tope;

    //Stack constructor
    Pilas(int n) {
        max = n;
        tope = -1;
        pila = new int[max];
    }

    //Method to push a value to stack
    void push(int valor) {
        pila[++tope] = valor;
    }

    //Method to pop a value from stack
    int pop() {
        return pila[tope--];
    }

    //Method to check if the stack is full
    boolean pilaLlena() {
        return tope == max - 1;
    }

    //Method to fill if the stack is empty
    boolean pilaVacia() {
        return tope == -1;
    }

    int seek() {
        return pila[tope];
    }

    **/* The problem that I have
     * Pilas aux= new Pilas(20)
     */**

    //Method to fill stacks in order
    void agregarEnOrden(int valor) {
        if (this.pilaVacia()) this.push(valor);
        else {
            while (valor < this.seek()) {
                aux.push(valor);
            }
            this.push(valor);
            while (!aux.pilaVacia()) {
                this.push(aux.pop());
            }
        }
    }
}

//Queue class
class FilaEstaticas {

    private int ar[];
    private int max;
    private int fin;

    //Queue constructor 
    FilaEstaticas(int t) {
        max = t;
        fin = -1;
        ar = new int[max];
    }

    //Method to check if the queue is empty
    boolean FilaEstaticaVacia() {
        return fin == -1;
    }

    //Method to check if the queue is full
    boolean FilaEstaticaLlena() {
        return fin == max - 1;
    }

    //Method to add a value to queue
    void agregar(int valor) {
        ar[++fin] = valor;
    }

    //Method to remove a value from queue
    int quitar() {
        int dato = ar[0];
        fin--;
        flota();
        return dato;
    }

    //method to move the position after removing a
void flota() {
        for(int i = 0; i <= fin; i++)
            ar[i] = ar[i + 1];
    }
}

class LaboratorioEstaticas {

    public static void main(String args[]) {
        int max = 10;
        FilaEstaticas fila = new FilaEstaticas(2 * max);
        Pilas p1 = new Pilas(10);
        Pilas p2 = new Pilas(10);
        int numero;

        while (!p1.pilaLlena()) {
            numero = (int)(Math.random() * 100);
            System.out.println("Numero agregado a la Pila " + numero);
            p1.agregarEnOrden(numero);
        }

        while (!p2.pilaLlena()) {
            numero = (int)(Math.random() * 100);
            System.out.println("Numero agregado a la Pila " + numero);
            p2.agregarEnOrden(numero);
        }

        while (!p1.pilaVacia() && !p2.pilaVacia()) {
            if (p1.seek() > p2.seek()) fila.agregar(p1.pop());
            else fila.agregar(p2.pop());

            if (p1.pilaVacia()) {
                while (!p2.pilaVacia()) {
                    fila.agregar(p2.pop());
                }
            } else {
                while (!p1.pilaVacia()) {
                    fila.agregar(p1.pop());
                }
            }
        }

        while (!fila.FilaEstaticaVacia()) {
            System.out.println(fila.quitar());
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这个答案主题详细讨论了这个问题 When is it ok to create object of a class inside a method of that class?

相关问题