something like that
brand = Dell or Toshiba or Lenovo
in java
something like that
brand = Dell or Toshiba or Lenovo
in java
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
.
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 .
Your question covers two cases, which are the following:
Example:
int valor = 0;
valor = 1;
valor = 2;
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.
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;
}