Java Vector Table Testing

Asked

Viewed 524 times

3

I’m having trouble solving the next code table test, someone could show me the output and how to 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 understood: I have one array (vector) with 5 numbers with positions from 0 to 4. I have a variable aux which is the same size as array (vector).

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

I don’t understand: aux[counter]++ within the first for. What is your true purpose?

I’m still a little bit in doubt when it comes to doing the table test, for when you ask counter++, i never really know the value I put in the table test.

That one counter++ do I add the value of my counter variable or do I have to skip so many boxes of the variable? Ex:

counter = 0;

counter++;

counter = 1;

Then I skip 1 house and print on the screen the value that is in the memory space 1 of the counter, isn’t it? But and when the counter becomes 2? I count from 1 in memory space or from 0?

1 answer

3


Can you understand that?

aux[counter] = aux[counter] + 1;

It is the same thing. It takes the value of the element indicated by counter of array aux, adds 1 and stores in the same location, evidently changing the value of this variable.

The counter++ only sum 1 to variable counter, It doesn’t do anything else. There’s no magic. There’s nothing like "bounce houses" or anything like that. The operator always works on the variable being applied, it doesn’t work on other things elsewhere. The vector is one thing and the counter is another, by chance at a certain time they are used together to obtain a result.

The term "bounce houses" there seems to me poorly employed. The counter is a variable, and aux is another, 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 values elements (some people give other names, like items, for example, but you have to be careful not to give names that indicate something else that is not). The elements of aux are accessed by a numerical index starting at zero for the first element up to a number before the size of the vector (since it starts from zero and not from one). How you will access in memory is a problem that in Java you do 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 will indicate its value. The access to the vector is random, so access what you want, is not sequence to jump anything.

If you don’t know what value to put on the table test you are probably doing the test wrong.

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

Some people prefer to use a grid and repeat the value of all variables on each line, whether they have changed it or not. In general this "requires" a marking to indicate better when there has been change or not.

You may be thinking about how to control the values of the elements of the vector, it is simple: consider each element as a single variable, because at the bottom it is that, 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 will move. If the counter is 1, it will move the variable aux[1], when you have to access to read or to change a value of aux, you have to look at the counter value, if it is 2, for example, you know you will manipulate the column aux[2]. Simple as that.

Has a table test example.

  • Oh understood, thank you... Very good explanation. I will try to do here. ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.