简体   繁体   English

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

[英]Can I declare an object inside its own class?

I'm learning data structures. 我正在学习数据结构。 I have tried to do an exercise to order two stacks with random numbers keeping always them in order from min to max passing comparing and passing the value to another stack called aux . 我试图做一个练习来对两个具有随机数的堆栈进行排序,使它们始终保持从最小到最大的顺序,这是通过比较并将值传递给另一个名为aux堆栈。 I have a method that fills them in order but I have a problem creating the object aux inside new class. 我有一个按顺序填充它们的方法,但是在新类内部创建对象aux遇到了问题。 I get the problem <init> . 我遇到了问题<init> I don't what that means. 我不是那个意思

PD: After the stacks are in order I pass them to a queue that always has to be in order from max to min. 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());
        }
    }
}

This answer thread discusses the problem in detail. 该答案线程详细讨论了该问题。
When is it ok to create object of a class inside a method of that class? 什么时候可以在该类的方法内创建该类的对象?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM