Setters in an array

Asked

Viewed 175 times

-1

I have a array of private integers separated into one class (Obs.: I did so to train the use of the get and of set) and another class that must set values at the addresses of array. This is a reduced example of my code for you to understand better

//Classe com os numeros a serem utilizados
public class Numeros {

    private int[] numeros = new int[]{
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
}

//GUARDA A COMBINAÇÃO DE 5 DEZENAS
public class Dezenas {
    private int[] dezenas = new int[2];
    //----------------------------------------------------------------------------------------------------------------

    public void setDezenas(int... dezenas) {
        this.dezenas = dezenas;
    }

    public int[] getDezenas() {
        return this.dezenas;
   }}

//FAZ TODAS AS COMBINAÇÕES POSSÍVEIS
public class Gerador {

    private void gerador() {
        for (int numero : numeros) {

            dezenas[0] = numero;

            for (int numero1 : numeros) {
                dezenas[1] = numero1;
            }}}}

The code in a single class works, but as I am training the access modifiers I want to find out how it is done. In the case of dezenas[] how would you set the address using the set? The way it is there is when everything is in the same class.

  • 1

    I do not understand your doubt. I understand what you are trying to do, but not the doubt.

  • Maniero, in this case is what would be the syntax of a set where will set the value of the memory address in for, like there are dozens[1]. I need to set the value of each array position and I don’t know how to do this using the set

  • The question is still confused about what this is, the comment speaks of something other than the question. I gave you an answer, see if this helps anything in trying to save the question. If it is not possible I will delete the answer and the question will unfortunately have to be closed because it is incomprehensible what is desired.

2 answers

2

I started answering and am posting initially not to miss the long answer, but I realized that the question is in unanswered condition, I will delete if it is not possible to keep. Do not consider this answer as correct yet, I will improve if it is possible to save the question. If you are not very curious or waste time reading yet, I hope soon to be able to leave in the definitive version

When you’re going to train something you’d better take something that makes sense to do. This example does a lot of things that it shouldn’t, probably because it’s just an example, but in software development when it does something meaningless you train to do something meaningless, you get used to it.

The idea of getters and setters is questioned. Not that can not be used, but almost always the use is abused. You can read more here, have a multi-question links.

It’s even more questionable when he gets one array. Incidentally getters and setters is an abstraction and it gets strange to use with such a concrete type in something abstract. It gives the impression that is creating the abstraction without knowing why. Don’t do anything in programming without knowing why you’re doing it. Everything needs to be justified and saying "I’m doing it because everyone else is doing it" is not a good reason. Then you should probably use a list, preferably a list interface. But again, you probably shouldn’t even have all this in the code.

This class organization also seems to make no sense. Classes must exist to solve a problem, must have a purpose, must not be almost random or just because you thought it was nice to put it like that. Even when it comes to naming them if they don’t make a lot of sense it’s already wrong.

In the way this code was written it does not say why it has these classes. If you cannot justify do not do any of this. I’m having trouble rewriting it because I don’t know what this is.

Note that the problem described in the comment has nothing to do with Setter as it is in the question. And I don’t even know if you really understood that one thing has nothing to do with another and that the Setter is not fit for this.

If you’re gonna change the array cannot use the for of data collection, it serves to take data, but not to assign values in it, access to these elements is read-only, so you need to use the for Brute who manually sweeps the entire collection. I would make a code if I had understood what combinations are these that you’re trying to make, I didn’t see how to match numerically.

It seems to me that all this should be one class. Maybe I should have one builder, but I’m speculating. It doesn’t seem like you should have Setter, gives the impression that wanted to put one anyway, even without sense, but it can be only because it is not well explained. It seems to me that the getter is a abstraction leak. An idea of code trying to match numbers, but it’s not even this, it looks like it wants to draw numbers, but anyway, it’s something:

public class Program {
    public static void main(String[] args) {
        Numeros objeto = new Numeros();
        objeto.gerador();
        for (int item : objeto.getDezenas()) System.out.println(item);
    }
}

class Numeros {
    private int[] numeros = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    private int[] dezenas = new int[100];
    public int[] getDezenas() {
        return this.dezenas;
   }
   public void gerador() {
        for (int i = 0; i < numeros.length; i++) {
            for (int j = 0; j < numeros.length; j++) dezenas[i * 10 + j] = i * 10 + j;
        }
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

In this example itself it should not even have all this, it was to be simpler.

The summary of the story: this example unravels. But I even found it useful because we can learn from the error, as long as it is criticizing it constructively.

0


For those with the same doubt as me, the solution is very simple. Imagine you create a private 3-space name array;

private String[] name = new String[3]; 

Now you want to access it in another class using a Setter. Create get normally, then create Setter as follows;

 public void setName (String name, int position){
     this.name[position] = name;
}

Now to set the values I will use a Scanner as an example:

System.out.println("Write the name: ");
    Scanner inName = new Scanner(System.in);
    colaborador.setNome(inName.next(),0);

Proto! For those who had doubts on how to set values in an Array is there an example.

Browser other questions tagged

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