Strange value when printing array

2

In java , I'm having difficulty understanding some concepts.

On a .jsp page, I have the following code:

  

String [] arrayRegioes =   request.getParameterValues ("numRegiaoUsuario"); // object

When I print the array, the value shown is:

[Ljava.lang.String;@5a879b45

I would like to know what this value means, because I can not see the value of it, since I have passed the following content:

[*, adm, r1]
    
asked by anonymous 24.03.2016 / 13:51

3 answers

3

What you are seeing is the default representation of Java objects, that is, instances of classes that do not implement toString() . Vectors are part of this group.

I can not see why you'd like to print a vector other than for debugging purposes. In this case, use a debugger. Debuggers usually have a way of viewing vectors and other enumerable objects and usually list the exact same elements you are wanting. Here has some instructions on how to use a debugger in the Eclipse. In other IDEs it is quite similar.

If you really want to print the vector value, whatever the reason, assuming you're using System.out.println() for this, just do:

for (String regiao : arrayRegioes) {
    System.out.println(regiao);
}

In this way, you are traversing the elements of the vector (calling them regiao ) and displaying.

    
24.03.2016 / 14:00
3

A compact way to print the array as desired is to use the Arrays.toString() utility method:

System.out.println(Arrays.toString(arrayRegioes));

[*, adm, r1] will be printed instead of the default [Ljava.lang.String;@5a879b45 representation.

    
24.03.2016 / 14:41
2

Try this:

for(String regioes : arrayRegioes){

System.out.print("arrayRegioes: " + regioes);
}
    
24.03.2016 / 14:00