How to insert data of type string in a list by means of a queue in JAVA?

1

I'm trying the following

List <String> enrolling = new ArrayList<String>();

String name;

enrolling.offer(name);
    
asked by Hiram Ramirez 20.04.2017 в 02:41
source

1 answer

1

The offer(E e) method is not used with a list. The offer(E e) method means offering a new element to a Queue with a content limitation. The Queue can reject the offer, returning false .

For the reason that ArrayList is a list (without limited capacity) and not a queue, offer is not implemented in lists.

In List you can use add(E e) to add elements of type E .

To use a queue, you use:

// construimos una cola con tamaño 10 elementos max
Queue<String> enrolled = new ArrayBlockingQueue<String>(10);

String name = "fulanito";

if (enrolled.offer(name)){
    // el nuevo elemento fue acceptado, trabajemos con eso
} else {
    // la cola estaba llena, así el elemento no fue aceptado.
    // podriamos hacer algo como:
    try{
        enrolled.put(name); // put espera hasta que queda espacio en la cola
    } catch (InterruptedException e){
        // todavía no logremos y más encima fuimos interumpido
    }
}

There are a variety of queue implementations for a multitude of uses, check the Queue interface for references to specific implementations.

    
answered by 20.04.2017 / 03:24
source