我正在遍历应该以这种方式显示的数组:

    k,j,i,h,g,f,e,d,c,b,a,
a,a,a,a,a,a,a,a,a,a,a,
b,b,b,b,b,a,a,a,a,a,a,
z,y,x,w,v,u,t,s,r,q,p,


但最后没有逗号。这就是我到目前为止所做的。

for ( int k = 0;  k < lineChar.length;  k++ ) //for printing in reverse order
            {
                if(lineChar[n] != ','){

                System.out.print(lineChar[n]);
                if(k == lineChar.length-1)
                   System.out.print("");
                else
                   System.out.print(",");

                --n;
                }
            }
            System.out.println();

最佳答案

int n=lineChar.lenght-1;

//Next 2 lines are edit after comment by @aix
while ((lineChar[n]==',') && (n>=0)) n--;
if (n>=0) System.out.print(lineChar[n--]);

for (;  n >=0;  n-- ) {
  if(lineChar[n] != ',') {
    System.out.print(",");
    System.out.print(lineChar[n]);
  }
}

09-15 15:22