简体   繁体   中英

In Java, can I change a variable name in a for loop?

Is it possible to change a variable name every time a for loop loops itself? `

for(int j=1;j<non-1;j++){
    int []newT=new int[non-j];
    for(int i=0;i<non-j;i++){
        newT[i]=pattern[i+1]-pattern[i];
        System.out.print(newT[i]+" ");
        System.out.println("");
        }}

` Can I make the newT change every time the loop repeats, like after the for loop repeats, newT becomes new1, then new2, new3 and so on?

Probably you can not name it new1 but you can use 2d arrays and name it newT[ ][ ] and use it like newT[1][ ]. For example:

  for(int i = 0;i < n; i++){
      for(int j = 0; j < n; j++){
         newT [i][j] = i*j;
         System.out.println(new [i][j]);
      }
  }

You can reach it now, as newT[1][ ], newT[2][ ] ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM