How to remove elements from an array of objects in java

1

The problem I'm having is the following: I'm modeling a basketball team with some functions like agaregarTitular() and agregarSuplente() . Now I want to implement the cambiarSuplenteComoTitular() method, for that I defined 2 arrays of type Jugador ; one of substitutes and one of substitutes but, once I make the change, I need to eliminate the player who changes the substitution arrangement; but I do not know how to do it.

public class Equipo{

private String nombre;
private int a=0;
private int b=0;    
Jugador cambio;
private Jugador titulares[];
private Jugador suplentes[];

public Equipo(String nombre, int ntitulares, int nsuplentes){
    this.nombre=nombre;
    try{
    if(ntitulares+nsuplentes==30){
    titulares=new Jugador[ntitulares];
    suplentes=new Jugador[nsuplentes];
    }}
    catch(ArrayIndexOutOfBoundsException ex){
    System.out.println("Error 0");
    }
}

public String getNombre(){
    return nombre;
}

public void dimeNombre(String nombre){
    this.nombre=nombre;
}

public void agregarTitular(Jugador j){
    for(int i=0;i<titulares.length;i++){
        if(titulares[i]==null){
            titulares[i]=j;
        }
    }
}

public void agregarSuplente(Jugador j){
    for(int i=0;i<suplentes.length;i++){
        if(suplentes[i]==null){
            suplentes[i]=j;
        }
    }
}

public boolean cambiarSuplenteComoTitular(long ncedula){
    boolean traspaso=false;
    for(int i=0;i<suplentes.length;i++){
        if(suplentes[i].getCedula()==ncedula){
                cambio=suplentes[i];
                    agregarTitular(cambio);
                    traspaso=true;
                }else{
                    traspaso=false;
                }
            }



    return traspaso;
}
    
asked by Floppy 12.12.2018 в 16:51
source

3 answers

1

First, I see that the signature of your cambiarSuplenteComoTitular method is incomplete, because it only receives a ncedula , and seeing only the name of the parameter it is not clear to me if it is the substitute one or the owner. Also the name of the method hits me a little, because it is over-understood that a change in basketball is always a substitute for a holder.

Since I see then, a typical case of the problem xy , I will answer what you are not asking:

I suggest a change in the signature of the method by something like:

public bool realizarCambio(long nCedulaSuplente, long nCedulaTitular)

So, the implementation, basically has to identify each Jugador in their array and exchange them, without removing nothing from any fix, as the number of players remains constant, say, something like this :

public bool realizarCambio(long nCedulaSuplente, long nCedulaTitular) {
  bool result = false;
  int idxSuplente, idxTitular;
  Jugador titular, suplente;
  //buscamos primero al suplente
  for (int i=0; i<suplentes.length; i++) {
    if (suplentes[i].getCedula() == nCedulaSuplente) {
      suplente = suplentes[i];
      idxSuplente = i;
      break;
    }
  }
  //buscamos ahora al titular
  for (int i=0; i<titulares.length; i++) {
    if (titulares[i].getCedula() == nCedulaTitular) {
      titular = titulares[i];
      idxTitular = i;
      break;
    }
  }
  //si encontramos a ambos
  if (suplente != null && titular != null) {
    //simplemente los intercambiamos en los arreglos
    titulares[idxTitular] = suplente;
    suplentes[idxSuplente] = titular;
    result = true;
  } 
  return result;
}

It is probable that the code has some error, I have written it right here in the editor of StackOverflow, it is to give an idea, that you understand it and make your own implementation.

    
answered by 12.12.2018 / 17:45
source
1

I give you an example in java 8 of how you can eliminate an object in an array of objects:

suplentes = Arrays.stream(suplentes).filter(j -> j.getCedula()!= ncedula).collect(Collectors.toList()).toArray(new Jugador[0]);

Basically it filters by the cedula number that it receives as a parameter and assigns the new list to the existing list of objects. Hope this can help you ! Greetings.

    
answered by 12.12.2018 в 17:35
1

In java you have ArrayUtils which you can use to delete an item from an array knowing the index.

I leave information here: link

Following the material, it would be as follows

public boolean cambiarSuplenteComoTitular(long ncedula){
    boolean traspaso=false;
    for(int i=0;i<suplentes.length;i++){
        if(suplentes[i].getCedula()==ncedula){
                cambio=suplentes[i];
                    agregarTitular(cambio);
                    ArrayUtils.remove(suplentes,i); // Elimino Suplente
                    traspaso=true;
                }else{
                    traspaso=false;
                }
            }
    return traspaso;
}

I hope you find it useful.

    
answered by 12.12.2018 в 16:59