I'm having trouble removing an element from an array of objects. The code I have would be the following:
private Plataforma[] plataformas;
public void eliminarPlataforma(String nombrePlataformaEliminar, int nClientes) {
//Buscar posicion de la plataforma a eliminar
for(int i=0; i<plataformas.length; i++) {
if(plataformas[i].getNombre() == nombrePlataformaEliminar) {
//Eliminar ese elemento
}
}
}
How would that element be removed?
Note that I can't import anything other than java.util.Arrays
.
Thanks in advance.
Keep in mind the following about Arrays or arrangements in java
ADVANTAGE
Access and operation with more efficient elements.
They allow to store both objects and primitive types directly
DISADVANTAGES
This indicates that the space already created in the array cannot be eliminated, so you should replace it with another data, a null for example
another piece of information is that the operator
==
that operates working with the addresses. Internally compares object references. On the other hand, the methodequals
compares the actual content of the instances. so you will need to use the methodequals
to correctly compare the dataif what you are looking for is to resize the array obtaining the different values of
null
in a new array you can use the methodfilter
I think you have the problem here:
In java you cannot compare strings in a direct way, you have to use the equals : method
String1.equals(String2)
. Using the == operator checks if the instances are the same, since Strings are not primitive data types. The code would be something like this: