Error in conditional

3

Hello, I need to create a queue using vectors in Java and I do not understand why the condition does not work when my vector is full.

import java.util.Vector;

public class ColaVector {
    private Vector<Integer> cola = new Vector<Integer> (3);
    public int elementosEncolados = 0;



    public void insertarAlFinal(int elemento) {

        if (this.elementosEncolados <= this.cola.size()) {

        this.cola.add(elemento);
        this.elementosEncolados++;

        } else {
            System.out.println("No se puede insertar. Cola llena");
    }

    public int primerValor() { 
       return cola.firstElement();
    }

}

Here is the main class from which I call all the classes shown in the previous table and as you can see my vector of 4 elements is full and when printing on the screen the first value should also show that it is full.

public class Main {

public static void main(String[] args) {

    ColaVector c1 = new ColaVector();


    c1.insertarAlFinal(1);
    c1.insertarAlFinal(2);
    c1.insertarAlFinal(3);
    c1.insertarAlFinal(4);
    c1.insertarAlFinal(5);
    c1.insertarAlFinal(6);
    c1.insertarAlFinal(7);


    System.out.println( c1.primerValor() );


    }

}
    
asked by Alvaromr7 05.05.2017 в 01:11
source

2 answers

1

The condition does apply but you think it does one thing, when it does another:

public void insertarAlFinal(int elemento) {

        System.out.println("" + this.elementosEncolados);
        System.out.println("" + this.cola.size());

        if (this.elementosEncolados <= this.cola.size()) {

        this.cola.add(elemento);
        this.elementosEncolados++;

        }

The above would have an exit more or less like this:

0
0
1
1
2
2
3
3

You can see that both elements and size grow at the same time, now without making many changes and seeing how the vector initializes you could use the following:

public void insertarAlFinal(int elemento) {     

        if (this.elementosEncolados + 1 < this.cola.capacity()) {

        this.cola.add(elemento);
        this.elementosEncolados++;

        } else {
            System.out.println("No se puede insertar. Cola llena");
    }
    }

We change this.cola.size() by this.cola.capacity() and <= by this.elementosEncolados + 1 < although the latter is only for your code can do what you are looking for you can change it and adjust it better. I think his main mistake was that maybe he was confused with size() and capacity()

The previous thing would have an exit thus:

0
3
1
3
2
3
No se puede insertar. Cola llena

By the way, you need a } in the method insertarAlFinal

  • size() returns the number of components on the vector in this case.
  • capacity() returns the current capacity of the vector.

Vector class

    
answered by 05.05.2017 в 01:43
1

First, you must use capacity () to get your initial capacity in your vector, then make some adjustments so that if you correctly validate the 3 elements that should enter the queue:

Note: The Vector class indicates an initial capacity and as a second argument the size that will increase:

When we create a vector or object of the Vector class, we can specify its initial dimension, and how much it will grow if we exceed that dimension.

Vector vector=new Vector(20, 5);
package com.stack.over.flow.ejemplos;

import java.util.NoSuchElementException;
import java.util.Vector;

public class ColaVector {

    private Vector<Integer> cola = new Vector<Integer>(3);
    public int elementosEncolados = 0;
    static int capacidadCola = 0;

    public void insertarAlFinal(int elemento) {
        try {

            if (this.elementosEncolados < capacidadCola) {

                this.cola.add(elemento);
                this.elementosEncolados++;

            } else {
                System.out.println("No se puede insertar. Cola llena");
            }
        } catch (NoSuchElementException e) {
            e.printStackTrace();

        } catch (Exception e2) {
            e2.printStackTrace();

        }

    }

    public int primerValor() {
        return cola.firstElement();
    }

    public static void getCapacidadInit() {
        ColaVector obj = new ColaVector();
        capacidadCola = obj.cola.capacity();
        System.out.println("capacidad es : " + capacidadCola);
    }

    public static void main(String[] args) {
        getCapacidadInit();
        ColaVector c1 = new ColaVector();

        c1.insertarAlFinal(1);
        c1.insertarAlFinal(2);
        c1.insertarAlFinal(3);
        c1.insertarAlFinal(4);
        c1.insertarAlFinal(5);
        c1.insertarAlFinal(6);
        c1.insertarAlFinal(7);

        System.out.println(c1.primerValor());

    }

}
    
answered by 05.05.2017 в 01:55