Foreign value when printing array

Asked

Viewed 182 times

2

In java, I’m having trouble understanding some concepts.

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

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

When printing the array, the value shown is:

[Ljava.lang.String;@5a879b45

I would like to know what that value means, because I cannot visualize its value, since I passed the following content:

[*, adm, r1]
  • How are you printing this amount?

  • System.out.print("arrayRegioes: " + arrayRegioes);

  • Now your question has radically changed. The answer no longer serves.

  • Diego, I create a new question. I really need this help. You can help me?

  • 1

    @durtto I suggest to reverse your question to what was before and make a new one. At least this will suit those who have the same doubt.

3 answers

3

Try it like this:

for(String regioes : arrayRegioes){

System.out.print("arrayRegioes: " + regioes);
}
  • I will supplement my question, you will understand where I am going.

2


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

I cannot see why you would like to print a vector other than for debugging purposes. In this case, use a debugger. Debuggers often have a mode of visualization of vectors and other enumerable objects and usually list the elements exactly the same as you want them to. Here has some instructions on how to use a debugger in Eclipse. In other Ides it is quite similar.

If you really want to print the value of the vector, whatever the reason, suppose you are 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 exhibiting.

  • I will supplement my question, you will understand where I am going.

  • An alternative way is to do System.out.println(Arrays.toString(arrayRegioes)). Will be printed [*, adm, r1] instead of [Ljava.lang.String;@5a879b45.

  • @Piovezan did not know this. Gives a good answer!

  • @Pabloalmeida Criada :)

2

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

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.