can I have different values for a variable? [closed]

0

something like that

brand = Dell or Toshiba or Lenovo

in java

    
asked by Patricio 06.11.2017 в 15:33
source

4 answers

2

What you are looking for is a listed type ( enum ). In the example that you put, you can create this enumeration:

public enum Marca
{
    DELL,TOSHIBA,LENOVO
}

And when creating your variable you would use that type:

Marca marcaOrdenador = Marca.DELL;

That way, in marcaOrdenador you could only store one of the three values that you defined in your type Marca .

    
answered by 06.11.2017 в 16:24
1

It's possible, it's a somewhat old design pattern.

First define constants for your values:

final static int DELL=1; //0001
final static int TOSHIBA=2; //0010
final static int LENOVO=4; //0100
final static int ALIENWARE=8; //1000

Then to be able to combine you use bit operators.

for example:

int marca = ALIENWARE | DELL; // 1001

It would indicate an alienware brand and dell at the same time to be a sub-brand.

You can use the same trick to process the values, for example:

public static String obtieneCategoria(int marcaComputadora)
{
  String descripcion;
  if(marcaComputadora & DELL)
  {
   descripcion+="Laptop";
  }
  if(marcaComputadora & ALIENWARE)
  {
   descripcion+=" Gamer";
  }
  return descripcion;
}

And in this way get the "Laptop Gamer" category by sending the function call as

System.out.println(obtieneCategoria(ALIENWARE | DELL));

Addendum: It is recommended to use EnumSet s instead of bit fields, but it is less verbose as well. If you want more info about it, I recommend you read this article in English link .

    
answered by 06.11.2017 в 17:10
1

Your question covers two cases, which are the following:

  • A variable can not have several values at the same time, that is, at a certain moment the variable will have a value, then, during the execution of the program, this value can change.
  • Example:

    int valor = 0;
    valor = 1;
    valor = 2;
    
  • If you mean that the variable can have several values at the same time, the answer is also no, but in this case, it would be a rather, not at all. This then exists something called Vector , which is a structure of data capable of saving values of a given data type in consecutive positions. It is important to keep in mind that you must define the size of the vector (the amount of data you will be able to save) and that the index will be from 0 to long - 1.
  • Example:

    String[] marcas = new String[3];
    marcas[0] = "DELL";
    marcas[1] = "HP";
    marcas[2] = "MAC";
    
    /*accedes al valor igualmente por el índice*/
    String val = marcas[0]; //a la variable val se le asigna el valor que tenía marcas en la posición 1.
    

    Note:

    There are more efficient ways to store multiple values in a variable, such as Enums , linked lists or any other type of data collection, but I think the vector would be enough for your problem.

        
    answered by 06.11.2017 в 19:42
    -1

    a variable has a value, what you can do is store it in an array and in the positions of this you save the values

    public static void main(String [ ] args){
    String [ ] variable = new String [3];
    variable[0] = dell;
    variable[1] = Toshiba;
    variable[2] = lenovo;
    

    }

        
    answered by 06.11.2017 в 16:15