I caught the FOR loop in the getColumnName method of the AbstractTableModel class.
Actually, here it is:

 public String getColumnName(int column) { String result = ""; for (; column >= 0; column = column / 26 - 1) { result = (char)((char)(column%26)+'A') + result; } return result; } 

(; column >= 0; column = column / 26 - 1) - this is not clear to me.

I would like to see an explanation.

The java version is "1.8.0_102".

  • and what exactly is not clear? - Grundy
  • @Grundy apparently frightened by the absence of something in statement1, where they usually write int i = 0 =) - Alexey Shimansky
  • @ AlekseyShimansky, well, the third block, in principle, is also not the usual i++ - Grundy
  • The usual mathematical operands, the count parameter obtained from the method constructor, and not as usual to see int i = 0 - GenCloud
  • @Grundy, like the second. for(;;); :) - Qwertiy

3 answers 3

This loop is executed as long as column >= 0 true. At each iteration of the loop, the value of the column variable changes in accordance with the expression:

 column = column / 26 - 1 

Perhaps you are confused by the fact that there is no initialization of any variables in the for loop record - this is normal practice. There are situations when you do not need to initialize anything here.

Now, regarding the actions directly inside the cycle:

 result = (char)((char)(column%26)+'A') + result; 

It happens here:

  • calculating the value of column%26 — the remainder of dividing column by 26 ;
  • (char)(column%26) - an explicit conversion of the value obtained in the previous paragraph to the type char ;
  • (char)(column%26)+'A' - character codes are added (char)(column%26) and 'A' ;
  • (char)((char)(column%26)+'A') - the result of the previous action is converted to type char ;
  • there is a concatenation of the result of the previous action with the string result ( and this is bad ).
  • (; - is this expression how to understand? If you can, rewrite this cycle with a normal for loop, for comparison. - Jürgen von Markoff
  • @ JürgenvonMarkoff, he is normal. It meets twice ; and any of the three parts is optional. Yes, even all 3: for(;;)System.out.println("Hi from infinite loop!"); - Qwertiy

loops in java are declared as follows:

 for(инициализация переменных; условия для остановки; действие выполняемое на каждом шаге){ // тело цикла } 

Actually, each design in the cycle may be missing.
Such a cycle is quite valid and compiled.

 for(;;); 

In your case, the initialization of the variables was skipped.

  • one
    определение переменных - actually not :) - Qwertiy
  • one
    Not "definition of variables", but rather "initialization". Which may include the definition of variables as well. And it may not be required at all, and then it will be empty. - Akina
  • Yeah, I agree, a small inaccuracy corrected. Thanks - Artem Konovalov

I did not watch the declaration of this class, but it seems that this method could be declared as static .

To make the method more readable, I replaced the magic number 26 with the expression 'Z' - 'A' + 1 .

In addition, the method uses redundant type conversion, which, as it seems to me, can be safely removed.

The result is the following method.

 static public String getColumnName(int column) { final int N = 'Z' - 'A' + 1; String result = ""; for (; column >= 0; column = column / N - 1) { result = (char)( column % N +'A') + result; } return result; } 

To make it easier to understand what this method does, you can run the demo program.

/ * package whatever; // don't place package name! * /

 import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Ideone { static public String getColumnName(int column) { final int N = 'Z' - 'A' + 1; String result = ""; for (; column >= 0; column = column / N - 1) { result = (char)( column % N +'A') + result; } return result; } public static void main (String[] args) throws java.lang.Exception { for ( int i = 0; i < 702; i++ ) { System.out.print( getColumnName( i ) + " " ); if ( ( i + 1 ) % 26 == 0 ) System.out.println(); } } } 

The output of the program to the console will look like this.

 ABCDEFGHIJKLMNOPQRSTU VWXYZ AA AB AC AD AE AF AG AH AI AJ AK AL AM AN AO AP AQ AR AS AT AU AV AW AX AY AZ BA BB BC BD BE BF BG BH BI BJ BK BL BM BN BO BP BQ BR BS BT BU BV BW BX BY BZ CA CB CC CD CE CF CG CH CI CJ CK CL CM CN CO CP CQ CR CS CT CU CV CW CX CY CZ DA DB DC DD DE DF DG DH DI DJ DK DL DM DN DO DP DQ DR DS DT DU DV DW DX DY DZ EA EB EC ED EE EF EG EH EI EJ EK EL EM EN EO EP EQ ER ES ET EU EV EW EX EY EZ FA FB FC FD FE FF FG FH FI FJ FK FL FM FN FO FP FQ FR FS FT FU FV FW FX FY FZ GA GB GC GD GE GF GG GH GI GJ GK GL GM GN GO GP GQ GR GS GT GU GV GW GX GY GZ HA HB HC HD HE HF HG HH HI HJ HK HL HM HN HO HP HQ HR HS HT HU HV HW HX HY HZ IA IB IC ID IE IF IG IH II IJ IK IL IM IN IO IP IQ IR IS IT IU IV IW IX IY IZ JA JB JC JD JE JF JG JH JI JJ JK JL JM JN JO JP JQ JR JS JT JU JV JW JX JY JZ KA KB KC KD KE KF KG KH KI KJ KK KL KM KN KO KP KQ KR KS KT KU KV KW KX KY KZ LA LB LC LD LE LF LG LH LI LJ LK LL LM LN LO LP LQ LR LS LT LU LV LW LX LY LZ MA MB MC MD ME MF MG MH MI MJ MK ML MM MN MO MP MQ MR MS MT MU MV MW MX MY MZ NA NB NC ND NE NF NG NH NI NJ NK NL NM NN NO NP NQ NR NS NT NU NV NW NX NY NZ OA OB OC OD OE OF OG OH OI OJ OK OL OM ON OO OP OQ OR OS OT OU OV OW OX OY OZ PA PB PC PD PE PF PG PH PI PJ PK PL PM PN PO PP PQ PR PS PT PU PV PW PX PY PZ QA QB QC QD QE QF QG QH QI QJ QK QL QM QN QO QP QQ QR QS QT QU QV QW QX QY QZ RA RB RC RD RE RF RG RH RI RJ RK RL RM RN RO RP RQ RR RS RT RU RV RW RX RY RZ SA SB SC SD SE SF SG SH SI SJ SK SL SM SN SO SP SQ SR SS ST SU SV SW SX SY SZ TA TB TC TD TE TF TG TH TI TJ TK TL TM TN TO TP TQ TR TS TT TU TV TW TX TY TZ UA UB UC UD UE UF UG UH UI UJ UK UL UM UN UO UP UQ UR US UT UU UV UW UX UY UZ VA VB VC VD VE VF VG VH VI VJ VK VL VM VN VO VP VQ VR VS VT VU VV VW VX VY VZ WA WB WC WD WE WF WG WH WI WJ WK WL WM WN WO WP WQ WR WS WT WU WV WW WX WY WZ XA XB XC XD XE XF XG XH XI XJ XK XL XM XN XO XP XQ XR XS XT XU XV XW XX XY XZ YA YB YC YD YE YF YG YH YI YJ YK YL YM YN YO YP YQ YR YS YT YU YV YW YX YY YZ ZA ZB ZC ZD ZE ZF ZG ZH ZI ZJ ZK ZL ZM ZN ZO ZP ZQ ZR ZS ZT ZU ZV ZW ZX ZY ZZ 

So, this method generates some names (column labels). How does it generate?

For example, consider an arbitrary number 71. To get the leftmost character, take the remainder of dividing by 26, which corresponds to the expression 'Z' - 'A' + 1 For this number we get

 71 = 2 * 26 + 19 

The number 19 (this expression in the body of the loop (char)( column % N +'A') ) corresponds to the letter 'T'

When dividing 71 by 26 we got 2 . So before the letter 'T' will be another letter. But since the indexing of letters comes from 0, not from 1, then 1 is subtracted from this expression.

 column = column / N - 1 

As a result, we get 1 , which corresponds to the letter 'B' ( 'A' + 1 = 'B' ) and will be obtained in the body at the next iteration of the cycle. After this, when dividing 1 by 26 is 0, that is, there are no more values ​​that contain another group of 26 characters. Therefore, we obtain that the cycle condition

 column = column / N - 1 

will give a negative number and the cycle will end.