How to avoid using dynamic variables / billions if instructions in Java?

So, since dynamic variables are nothing in Java, and if the instructions are terribly cumbersome, they looked for help, turning this block of code into a more concise one.

I looked at the hashmaps and they just didn't seem completely correct, but I probably didn't understand them.

public String m1 = "Name1";
public String m1ip = "192.1.1.1";
public String m2 = "Name2";
public String m2ip = "192.1.1.1";
public String req;
public String reqip;

... edit the code ...

if (requestedMachine == 1)
{ req = m1; reqip = m1ip;}
else if (requestedMachine == 2)
{ req = m2; reqip = m2ip;}
else if (requestedMachine == 3)
{ req = m3; reqip = m3ip;}
else if (requestedMachine == 4)
{ req = m4; reqip = m4ip;}
else if (requestedMachine == 5)
{ req = m5; reqip = m5ip;}

requestMachine will be an integer that determines what values ​​req and reqip should be assigned.

Thanks in advance.

+2
source share
5 answers

Define the Machine class containing the nameand field ip. Create an array of the machine. Enter the machine located at the index requestedMachine(or requestedMachine - 1, if the number starts with 1):

Machine[] machines = new Machine[] {
    new Machine("Name1", "192.1.1.1"),
    new Machine("Name2", "192.1.1.1"),
    ...
}
...

Machine machine = machines[requestedMachine - 1];
+8

Machine:

class Machine {
    String name;
    String ip;
    //Constructor, getters, setters etc omitted
}

:

Machine[] machines = ... //initialize them with values

, requestedMachine:

Machine myMachine = machines[requestedMachine];
+2

:

/**
   <P>{@code java EnumDeltaXmpl}</P>
 **/
public class EnumDeltaXmpl  {
  public static final void main(String[] ingo_red)  {
     test(MachineAction.ONE);
     test(MachineAction.TWO);
     test(MachineAction.THREE);
     test(MachineAction.FOUR);
  }
  private static final void test(MachineAction m_a)  {
     System.out.println("MachineAction." + m_a + ": name=" + m_a.sName + ", ip=" + m_a.sIP + "");
  }
}

enum MachineAction  {
   ONE("Name1", "192.1.1.1"),
   TWO("Name2", "292.2.2.2"),
   THREE("Name3", "392.3.3.3"),
   FOUR("Name4", "492.4.4.4"),
   FIVE("Name5", "592.5.5.5");

   public final String sName;
   public final String sIP;

   private MachineAction(String s_name, String s_ip)  {
      sName = s_name;
      sIP = s_ip;
   }
}

:

[C:\java_code\]java EnumDeltaXmpl
MachineAction.ONE: name=Name1, ip=192.1.1.1
MachineAction.TWO: name=Name2, ip=292.2.2.2
MachineAction.THREE: name=Name3, ip=392.3.3.3
MachineAction.FOUR: name=Name4, ip=492.4.4.4
0

- IP, .., .

public class Machine(){
private String name, ip;
public Machine(String name, String ip){
   this.name=name;
   // You can check a valid ip
   this.ip=ip;
}}


public class Machines(){
private Machine[] machines;
private int number_of_machines;
public Machines(){
  //define number_of_machines for your array and length of itself
}}
main()
Machine[] Machines = new Machine[number_of_machines];
Machine m1 = new Machine(String name, String ip);
.
.
.
Machine mn = new Machine(String name, String ip);
int number=5;
for(int i=0; i<number_of_machines; i++){
   if (machines[number]<number_of_machines){
      System.out.println("There is no machine with that number");
   }else if (machines[number]==number_of_machines-1){
      System.out.println("That is the choosen machine");
   }
}

}

0

id 0 , HashMap. -

HashMap<Integer, Machine> machines = new HashMap<>();
machines.put(1, machine1);
machines.put(7, machine7);
...

Machine machine7 = machines.get(7); 

, , . id 0,1,2,3,4,5,... , .

0

Source: https://habr.com/ru/post/1533776/


All Articles