Why does NetBeans suggest changing an array addition?

1

I'm starting my studies in programming with Java. I'm creating a class for object in NetBeans that alerts the following message:

  

Array concatenated with String

Why?

My code:

import java.util.Arrays;

public class PessoaGenerica {
    int idade[]
    String nome[];
    String cidade[]

    void dizerNome(){
       System.out.println ("OLá, meu nome é" + Arrays.toString(nome));
    }

    void dizerIdade(){
       System.out.println ("OLá, meu nome é" + Arrays.toString(nome));
    }

    void dizerCidade(){
       System.out.println ("OLá, meu nome é" + Arrays.toString(nome));
    }
}

    
asked by anonymous 25.04.2016 / 00:52

1 answer

3

I think I could start by reading what is a vector .

It even makes sense to pass the whole vector as a parameter, but it makes little sense to access it directly. We usually access its elements through an index. Then it would make sense to do, for example nome[0] for the first element of the vector, or nome[1] for the second.

I could even access through a variable if it exists somewhere. It could be a loop that goes varying to get all the elements (although in this case it has for that does not even need to get the index, but this is a more advanced subject): nome[i] .

Of course, to access an element, you must have created it. In this code you are not creating anything. It is not even allowing space for vectors to have elements. Ideally, they should be declared with the maximum amount of elements it can withstand. Or use a ArrayList to grow the number of elements as needed.

It's still a bit odd to have one vector for nome , another for cidade and another for idade . This information seems to be correlated and should be put together in a separate structure, and the vector would be of the type created with these members. I will not go into detail because it seems to be advanced to what you know. But this is why you should go with simpler examples, maybe look for a book that will guide you best to learn everything. For now try to use vector only for one thing at a time and hope to better understand the creation of classes to create vectors with composite information.

Actually watching the class, I do not even know if I should have vectors there. The problem may be this. He may have declared class members as vectors and was not what he wanted. Try taking out the brackets and see what happens (obviously taking out Arrays.toString() too). Maybe it's the right way. Once you understand this work well, you might think about creating vectors in another class. I do not think it's a good idea to have a vector. If it was the intention, I think you're learning to do it the wrong way. I can not imagine why I would want to save several people inside a class that has a name that seems to be a single person.

I reinforce the idea of looking for a structured and quality way of doing each thing, that teaches the basics with good didactics and without being cake recipe. I know it's not easy to find, you have to be very careful because there's a lot of bad things out there. Here it is not suitable to ask for references on this. Start with familiar books. Be critical of everything. Try different sources. Ask about specific questions you already have experience. Can be here. For this the site is very good.

    
25.04.2016 / 01:28