Modify vector inside java method


The purpose of the program is to create a Marine Life Simulator (in broad strokes).

My problem is this:

I have an abstract class called AnimalMarino that defines a few methods, including this one:

public abstract int getVelocidad();

This method returns an integer. Up here all good. After several more things, I must implement the feeding, where the upper tier of the pyramid eats the immediately lower one, and eats the slower one. My idea, then, has been to order the vector of the affected animals according to the speed in order to select which ones will be the least lucky and will be devoured. To sort the vector, I thought about using quicksort:

 * quicksort (Vector<AnimalMarino>).
 * Metodo que ordena un vector de Animales Marinos segun su velocidad
 * @param v Vector a ordenar
private void quicksort(Vector<AnimalMarino> v) {
    this.partition(v, 0, v.size()-1);

Partition processing:

 * partition (Vector<AnimalMarino>,int,int).
 * Funcion auxiliar que ordena una particion de quicksort.
 * @param v     Vector a ordenar
 * @param izq   Indice izquierdo
 * @param der   Indice derecho
private void partition(Vector<AnimalMarino> v, int izq, int der) {
    int i, j;
    int piv;          ///< Pivote. Será la velocidad sobre la que pivotará
    AnimalMarino aux;

    piv = v.elementAt((izq+der)/2).getVelocidad();
    i = izq;
    j = der;

    while (i <= j){
        while(v.elementAt(i).getVelocidad() < piv)
        while(v.elementAt(j).getVelocidad() > piv)

        if(i < j){
            aux = v.elementAt(i);
            v.add(i, v.elementAt(j));
            v.add(j, v.elementAt(i));
        } else{
            if (i == j){

    if (izq < j)
        this.partition(v, izq, j);
    else if (i < der)
        this.partition(v, i, der);

My question is: will this vector that I am passing through change at the end of the whole, or will it remain the same? Will I need to ask that the ordered vector be returned or is it not necessary? Thanks in advance.

asked by Slifer Dragon 07.11.2016 в 21:00

1 answer


In java the parameters are passed by value, which means that a copy of the object is made. But you must be careful because a superficial copy is made and using the methods you can modify the state of the object passed as a parameter

answered by 08.11.2016 в 10:14