How does this code work?

  • 0
    I am learning Java. Trying to read the code. This is the Shield method:

    public void put (char ch) {
    if (putloc == q.length - 1) {
    System.out.println ("- The queue is full");
    putloc ++;
    q [putloc] = ch;

    I don't understand why increment putloc if it is followed by a method parameter (ch)?
    Java Avery Huerta, Dec 6, 2019

  • 2 Answers
  • 0
    if followed by a method parameter (ch)?

    here is a mistake. Nothing is assigned to putloc. Assigned to q.
    Brielle Shelton

  • 0
    public void put(char ch) {
    if (putloc == q.length - 1) {
    System.out.println(" - Очередь заполнена");
    q[putloc] = ch;

    Well, let's read together.

    There is a variable putloc that stores the index of the last placement in the queue, I assume that with an empty queue, putloc = -1

    There is an array q in which we store our queue, of a certain size.

    When adding an element to the queue, we check if the index of the last element placed in the queue is equal to the maximum index of the array q , and if it is, then we swear and do nothing.

    If the array is not yet filled, we determine the index of the array at which to place the new element of the queue, by incrementing the putloc variable.

    And then we put the received value ch into the array at the new index putloc .

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!