Vector within vector. What does it do in this context?

Asked

Viewed 950 times

7

Forty students were asked the level of quality of food in the student canteen, on a scale from 0 to 10. ( 1 meaning horrific and 10 meaning excellent). Put the 40 responses into an entire array and summarize the search results.

    public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    final int tamanhoResposta = 40, frequencySize = 11;
    int response[] = {5, 10, 6, 7, 3, 7,5 ,6 ,3, 10, 4,5,4,9,8,7,5,6,5,6,4,5,6,7,8,4,3,8,9,10,9,6,5,3,9,5,9,10};
    int frequency[] ={0};


    for(int i = 0; i < tamanhoResposta; i++){
        ++frequency[response[i]];
    }

    System.out.println(" Avaliação ===================== Frequência " );
    for(int ii = 0; ii < frequencySize; ii++){
        System.out.println(ii + " ");
        System.out.println(frequency[ii]);
    }

I don’t understand what this part is for, but what it does?

++frequency[response[i]];

He makes that exception:

Exception in thread "main" java.lang.Arrayindexoutofboundsexception: 5

  • 4

    The downvoter could take advantage that came by and answer the question or explain how it can be improved.

  • downvoter? I don’t understand...

  • 2

    @Alinegonzaga his question had 5 positive and one negative votes, totaling 4 points so far. What Caffé suggested in his comment, is that the downvoter (the person who voted negative) came forward to help resolve or improve the issue.

  • So do I. Thanks for explaining the term.

1 answer

11


The operator ++ is an incrementer. If you have a variable called x can do:

x++;

that the same as:

x += 1;

which is the same as:

x = x + 1;

which is the same as:

++x;

The difference is that the latter increments one before using the variable value. This is useful in an expression, but not in a statement, as is the case used. That is, in that case could have written frequency[response[i]]++; that gave in the same. Or could have written frequency[response[i]] = frequency[response[i]] + 1;. A lot longer, right?

Obviously what he’s incrementing is an element of array frequency. Knows arrays, right? What is this element? Is it the first element? That is, it is the 0? It is the 1? It is determined by a variable. In this case it is the response[i];, which in turn is also an element of a array. So he’s taking the element of response determined by i, and this element is being used as an index of frequency.

N]ao confuses the array with its elements. All are variables. The array is a variable that contains a portion of variables, but each element is an individual variable.

A different way of writing the same thing:

int x = response[i];
int y = frequency[x];
y = y + 1;
frequency[x] = y;

It’s become clearer now?

  • It became clearer, but why would I put a vector inside the same vector? I need to read just a little bit more about it and choose the answer...

  • 2

    First, what is the problem of having a vector inside another vector? A vector is just a collection of data. What data can you have inside a vector? Anything can be there. Including another vector. A matrix is not the vector of vectors? This is a simplification, but it’s a way of understanding the issue. But in this case it’s not even using one vector within another. You are using an element of an index vector of an element of another vector. It is the information you have. Can you do table testing? This helps to understand a lot.

  • Rs. :) I am beginning to understand. I will test.

  • 1

    To an element of frequency. You can’t tell by the unblemished example I made?

  • Sorry, now I saw it. I was a little reckless.

  • Um, from what I understand it counts how many times the occurrence of a number and stores...in the vector...

  • Yes. And don’t change the question halfway.

  • You don’t want to help me mustache anymore?

  • I can help, yes, but in another question, this, as it was originally asked, already has a solution. You can’t keep changing the question to for all the code problems. If it still has a problem, ask another question.

  • CERTAIN! ^------^

Show 5 more comments

Browser other questions tagged

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