using as a reference this tutorial
we have that:
tipo_de_dato[] nombre_del_array;
this defines the array for example in your case
Jugador titulares[];
Now that's fine but we have a small problem in the way you create the array
private int a=0;
private Jugador titulares[]=new Jugador[a];
this is the same as saying:
private Jugador titulares[]=new Jugador[0];
We create an array of players that can not contain players because it is of size 0
and in this case is not ideal. because we can not add anything. in this case I would like to receive or determine the number of fields that will be necessary to reserve in this array
now to apply or create agregarTitular(Jugador j1)
ocurine 2 ... so you see 3 ways to do it. This will depend a lot on how you want it to work. I will suggest 2 ways.
agregarTitular(Jugador j1)
add players as they are
enter therefore the first player remains in the position 0
(remember array starts from 0) and so on until you reach the position
titulares.lenght -1
(the last value you can add) and when it is full, do not allow adding more? or say false? that depends on you ...
agregarTitular(Jugador j1, int posicion)
add or replace a player in the position provided so that titulares[posicion] = j1
if there is already a player in titulares[posicion]
this is replaced by j1
BUT in this case you should verify that posicion
is a valid index
in this case to add an element in the array what we have to do is:
public class Equipo{
private String nombre;
//aque SOLO se define las variables no se inicializan (crean)
private Jugador titulares[];
private Jugador suplentes[];
/**
* el contructor de esta clase crea un nuevo objeto el parametro ntitulares
* nos define cuantos titulares va a tener el parametro nsuplentes nos
* define cuantos suplentes va a tener
*
* @param nequipo nombre inicial
* @param ntitulares cuantos titulares va a tener
* @param nsuplentes cuantos suplentes va a tener
*/
public Equipo(String nequipo,int ntitulares, int nsuplentes){
//...
//agregar codigo aqui que verificque que
//ntitulares + nsuplentes == 30
// y que maneje los esenario donde ntitulares + nsuplentes != 30
// o disparar error? eso depende de OP
//...
//se le asigna el nombre del equipo
nombre = nequipo;
//se crea y asigna un array a titulares del tamano ntitulares
titulares = new Jugador[ntitulares];
//se crea y asigna un array a suplentes del tamano nsuplentes
suplentes = new Jugador[nsuplentes];
}
public String getNombre(){
return nombre;
}
// cambia el nombre del equipo
public void setNombre(String nombre){
this.nombre=nombre;
}
/**
* metodo 1 agrega el jugador en la posicion que se provee
*
* @param j1 el jugador a agregar en la posicion
* @param posicion la posicion donde va el jugador
* @return true si se agrego el jugador false si no se puede agregar (la
* posicion no es valida)
*/
public boolean agregarTitular(Jugador j1, int posicion) {
if (posicion >= 0 && posicion < titulares.length) {
titulares[posicion] = j1;
return true;
}
return false;
}
/**
* metodo 2 agrega el jugador en el array si existe una posision cuyo valor sea nulo (campo vacio)
* @param j1 jugador a agregar
* @return true si se pudo agregar false si el array ya esta lleno
*/
public boolean agregarTitular(Jugador j1) {
for (int i = 0; i < titulares.length; i++) {
if (titulares[i] == null) {
titulares[i] = j1;
return true;
}
}
return false;
}
//para saber cuantos campos hay para titulares
public int camposTitulares(){
return titulares.length;
}
}
and now how to use this class? Simple to create a new equipment object:
// esto crea un equipo con el nombre "Liga Deportiva X"
// de 26 jugadores titulares y 4 suplentes
Equipo miEquipo = new Equipo("Liga Deportiva X", 26,4);