Java Table Table Test

3

I'm having difficulty solving the table test of the following code, could someone show me the output and how do I do it?

public class Principal
{

    public static void main(String args[]) {

       int array[] = {1, 2, 3, 4, 5};
       int aux[] = array;

       for(int counter = 0; counter < aux.length; counter++) {

          aux[counter]++;

       }

       for(int counter=0; counter < array.length; counter++) {

             System.out.println(++array[counter]);

       }

    }

}

What I understand: I have an array (vector) with 5 numbers with positions 0 through 4. I have a aux variable that is the same size as the array (vector).

I understood that array.length is to get the size of the vector, that is to say the maximum size of my vector, which in this case is 5 .

I did not understand: aux[counter]++ within first for . What is your true purpose?

I'm still a little hesitant when it comes to doing the table test , because when you ask for counter++ , I never really know the value I put on the bench test.

Does this counter++ add the value of my counter variable or do I have to skip so many houses of the variable? Ex:

counter = 0;

counter++;

counter = 1;

Dai leap 1 home and print on the screen the value that is in the memory space 1 of the counter, is not it? But when counter becomes 2? Do I count from 1 in memory space or from 0?

    
asked by anonymous 24.03.2016 / 22:47

1 answer

3

Can you understand this?

aux[counter] = aux[counter] + 1;
It's the same thing. It takes the value of the element indicated by counter of the array aux , adds 1 and saves in the same place, evidently changing the value of this variable.

The counter++ only adds 1 to the counter variable, it does nothing else. There is no magic. There's nothing like "skipping houses" or anything. The operator always works on the variable being applied, it does not work on other things elsewhere. The vector is one thing and the counter is another, by chance at some point they are used together to get a result.

The term "jumping houses" seems to me to be misused. The counter is a variable, and aux is another, they are different things. The variable aux has a special situation, it does not have a single value, it has several. We usually call each of these elements values (some people give other names, such as items, but you have to be careful not to give names that indicate something else that is not). Elements of aux are accessed by a numeric index starting at zero for the first element up to a number before the size of the vector (since it starts from zero instead of one). How to access in memory is a problem that in Java does not need to know, just think abstractly.

You do not skip houses, you access an element by a number. This number can be a literal or a variable that indicates its value. Access to the vector is random, so access what you want, it's not a sequence to skip anything.

If you do not know what the value put on the table test is probably doing the test wrong.

Table test is to follow the execution sequence of the code as if it were the computer and have columns (or other organization) for each variable and there goes by noting the value of the variable whenever it changes. You know that the current value is always the last, This should simulate the memory of the computer.

Some people prefer to use a grid and repeat the value of all variables in each row, whether or not they have changed. In general this "requires" a markup to indicate better when there was change or not.

You may be thinking of how to control the values of vector elements. It's simple: consider each element as a single variable, because deep down, that's it, there are several variables with the same name and an index to differentiate them. You'll have to put their names in the header: aux[0] | aux[1] | etc. .

The current value of the counter will be used to determine which column you are going to play. If the counter is 1, it will move the variable aux[1] , when it has to access to read or to change a value of aux , it will have to look at the counter value, if it is 2, for example, it knows that it will manipulate the column aux[2] . Simple as that.

You have a table test example .

    
24.03.2016 / 23:09