Probably or your method getNome()
is returning a value other than intended or the array is empty or the item you intend to remove is not present. Without the array-related code it is not possible to evaluate precisely.
What I was able to do with this fragment of code that you published was test the execution logic, where I switched the array
by a List<String>
and populated listaCliente
uppercase.
import java.lang.*;
import java.util.*;
public class HelloWorld
{
public static void main(String[] args)
{
OtherClass myObject = new OtherClass();
System.out.print("Foram removidos " + myObject.removerTodosClientesPorNome("J") + " itens.");
}
}
public class OtherClass
{
List<String> listaCliente = new ArrayList<String>();
public OtherClass()
{
listaCliente.add("A"); listaCliente.add("B"); listaCliente.add("J");
listaCliente.add("A"); listaCliente.add("B"); listaCliente.add("J");
listaCliente.add("A"); listaCliente.add("B"); listaCliente.add("J");
}
public int removerTodosClientesPorNome(String nome) {
int quantidade = 0;
for (int i = 0; i < listaCliente.size(); i++) {
if (listaCliente.get(i) != null && listaCliente.get(i).equals(nome)) {
listaCliente.set(i, null);
quantidade++;
}
}
return quantidade;
}
}
whose result was:
Foram removidos 3 itens.
Put your full code.
– Bruno