I need to reverse a vector

Asked

Viewed 41 times

0

I need to know how to change this logic to receive vectors of any size.

public void inverter(int[] vetor) {
    int i;
    int temp;

    for(i=0; i < 3; i++){
    temp = vetor[i];
    vetor[i] = vetor[8-i];
    vetor[8-i] = temp;
    }


}

Ex: int[] vector 1 = new int[]{1,2,3,4,5,6,7,8,9};

//Prints [9,8,7,6,5,4,3,2,1]

  • 1

    Dude I’m new here and I don’t understand much how it works. What should I do to get this help I posted. Thanks

  • When a question is marked as duplicate, it is because it already has an answer. You accessed the links I indicated in the other question?

  • enters the Stackoverflow goal in Portuguese it has all the rules, usually in the beginning personal and very rude , more accustomed

  • @Marcelobatista who was rude here? Closes without saying anything complain, closes and a touch the author, is rude, it is too difficult to please here.

  • I didn’t talk about Oce, in general this is the perception with any weaker question say

  • Personal calm rsrs

  • This solves the problem https://answall.com/a/64830/3635 ?

  • @Guilhermenascimento even helps, but I need it to be a void method with no return.

  • @Snsilva gives a look https://pt.meta.stackoverflow.com/questions/5483/manual-de-como-n%C3%83o-ask-questions

  • Thanks @Marcelobatista I’ll take a look.

  • But why does it have to be void or not? After all it is only a method that you will add to your class for a specific and unique use, I see no sense or need for this to be void, after all you can create more than one method, as many as you want and each for a need ;)

  • It is an activity that the teacher has passed in repl.it and the method that is there is void and I can’t change it, I just have to make logica within that method @Guilhermenascimento.

  • @Snsilva wants no return, instead of return, display with System.out.prinln. What matters in the answer there is not the return, is you understand how it was done and reproduce as your exercise asks.

  • Got it here @Articuno, thanks.

  • Dear I did here and it worked, but I would have to reverse the vector itself without having to create another?

  • Snsilva made an example: https://repl.it/@inphinit/snsilvajava - it works like this, it clones the object (it is necessary to do this because of the reference of the object, so there is no setting vetor[i] = vetor[8] without affecting the original array causing the values to conflict. The other example without copying/cloning array and using native methods is this: https://repl.it/@inphinit/snsilvajava - cc @Articuno

  • 2

    You can do it without having to create a new one, your question logic is right, just abstract the size of the array, see: https://ideone.com/bxYEt8 c/c @Guilhermenascimento

  • @Sensational trick! D

  • @Articuno this was exactly what I needed, thank you very much. How do I vote on this solution as resolved?

  • @Guilhermenascimento was worth the service, was nice example in repl.it.

Show 15 more comments
No answers

Browser other questions tagged

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