Array [] print

0

I have a problem printing a String [] that is inside an ArrayList of objects when printing some prints as null.

Pasaporte - Jose Gregorio Silva Guedez.PDF
C:\Users\Javier Marín\Documents
5
Votantes: [
null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, ]
------------
Antecentes penales Apostillados - Jose Gregorio Silva Guedez.PDF
C:\Users\Javier Marín\Documents
6
Votantes: [
ramses, antonieta, marlyn, weber, simon, andres, null, null, null, null, null, null, null, null, null, null, null, null, null, null, ]
------------

More or less like that I get some if you print your content and others do not.

So I send it to print in the main

for(int i=0; i<p.listCanc.size();i++){
    MetodosListCanc.impObj(p.listCanc.get(i));
}

I have already tried in several ways and I still give error here I leave the method that prints

public static void impObj(InfCanc i){
    System.out.println(i.getName());
    System.out.println(i.getLoc());
    System.out.println(i.getVal());
    System.out.println("Votantes: [");
    String sl []=i.getVotantes();
    for(int j=0; j<20;j++){
        System.out.print(sl[j]+", ");
    }
    System.out.println("]");
    System.out.println("------------");
}
/*public static void impObj(int i){
    System.out.println(list.get(i).getName());
    System.out.println(list.get(i).getLoc());
    System.out.println(list.get(i).getVal());
    System.out.println("Votantes: "+Arrays.toString(list.get(i).getVotantes()));
    System.out.println("------------");
}*/

in both ways I get the same printing error with some objects.

    
asked by Javier Marin 21.09.2018 в 20:15
source

1 answer

2

to what I understand about your question and the structure of the objects, your problem seems to be the stop condition of the cycle, here:

 for(int j=0; j<20;j++)

In the impObj method, try changing j<20 to j<sl.length() , length gives you the size of the array. And also validates that the field is not null, within the cycle it places:

if(sl[j] != null)
    System.out.print(sl[j]+", ");

It would be something like:

public static void impObj(InfCanc i){
    System.out.println(i.getName());
    System.out.println(i.getLoc());
    System.out.println(i.getVal());
    System.out.println("Votantes: [");
    String sl []=i.getVotantes();
    for(int j=0; j<sl.length();j++){
        if(sl[j] != null)
            System.out.print(sl[j]+", ");
    }
    System.out.println("]");
    System.out.println("------------");
}
    
answered by 21.09.2018 в 20:26