Help with validating an arraylist if it is empty

0

I have a problem and I do not know how to validate that an arraylist of objects is not null, I have worked static fixes but I do not know how to use dynamics.

IF YOU CAN HELP ME WITH PSEUDOCODIGO, I thank you.

    
asked by Edgar Lopez Acevedo 20.08.2017 в 14:28
source

2 answers

4

In java, check that it is null or if the size() method, which returns the amount of element in array, returns 0:

ArrayList<String> nombres = new ArrayList<String>();

if(nombres == null || nombres.size() == 0)
{
  // el arraylist no tiene valor
}
else{
 // el arraylist tiene valor
}

If it's c #, it would be the same only if you replaced the method size() with Count that gives you the total element in the ArrayList:

 ArrayList nombres = new ArrayList();

if(nombres == null || nombres.Count == 0)
{
  // el arraylist no tiene valor
}
else{
 // el arraylist tiene valor
}
    
answered by 20.08.2017 в 14:38
1

The first thing you have to check is if it is null, this is because if it is null and you try to check through its methods if it is empty it will give you NullPointerException. In the same check you can use isEmpty () (Java) to check if it contains objects, this method returns a boolean

It would stay like this:

if(miLista == null || miLista.isEmpty())
{
//lista nula o vacía
}

This way, if it is null, the content of the if would be executed without asking if it is empty because we use double | If it is not null, the if query the second condition and know if it is empty.

    
answered by 20.08.2017 в 19:32