java - Run same class 5 times -


i've wrote complicated algorithm in java code. however, want run code class 5 times without doing self. called t1 class t2 class , put t1 class in loop 5 times. problem java save old values code , use them in next loop. question how run t1 class 5 times every time each time new values (without using 0 static variables) @ end of t1 code. here simple example i've done: package test;

public class t1 {     public static int n, m, o, i, j;     public static double t;     public static int[][]  v = new int[100][100];     public static int[]  c = new int [100];      public static void main(string[] args){         m=o+1; n=o+1; o=m+n;         for(j = 0 ; j < n ; j++){             for(i = 0 ; < m ; i++){                 v[i][j] = i+j+o;                 system.out.print(v[i][j]+"   ");             }                            system.out.print("\n");         }         system.out.println("--------");         for(j = 0 ; j < n ; j++){             c[j] = j+o;             system.out.print(c[j]+"   ");         }         system.out.println("\n--------");         system.out.println("o = "+o);         system.out.println("*******************************");     } } 

package test;  public class t2 {     public static void main(string[] args) {         for(int =0 ; <5 ; i++){             t1.main(args);         }     } } 

the result these codes are:

2    -------- 2    -------- o = 2 ******************************* 6   7   8    7   8   9    8   9   10    -------- 6   7   8    -------- o = 6 ******************************* 14   15   16   17   18   19   20    15   16   17   18   19   20   21    16   17   18   19   20   21   22    17   18   19   20   21   22   23    18   19   20   21   22   23   24    19   20   21   22   23   24   25    20   21   22   23   24   25   26    -------- 14   15   16   17   18   19   20    -------- o = 14 ******************************* 

while want them following (without put o=0 @ end of t1 class):

2    -------- 2    -------- o = 2 ******************************* 2    -------- 2    -------- o = 2 ******************************* 2    -------- 2    -------- o = 2 ******************************* 

you have defined in t1 static means variables bound class , not instances of class. need remove static specifiers fields , methods , create new instance in each loop iteration. not put code in main, instead in other method wich not staitc.

for(int =0 ; <5 ; i++){     new t1().process(args); } 

if need keep variables static introduce static reset() method set variable default values (the values had before first call) , call method after each iteration.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -