简体   繁体   English

Java中的初学者 - 字符串格式=“| % - ” + maxW [J] +的‘s’; - 这个字符串做什么?

[英]Beginner in Java - String format = “ | %-”+maxW[j]+“s”; - What does this string do?

I am a noob in Java and I came across the code below and couldn't figure out its function. 我是Java中的菜鸟,我遇到了下面的代码,无法弄清楚它的功能。 maxw[] is an array of type int. maxw []是int类型的数组。 row[] is an array of type String. row []是String类型的数组。

             String format = " | %-"+maxW[i]+"s";
       System.out.printf(format,row[i]);

My questions is : For this statement : System.out.printf(format,row[i]); 我的问题是:对于这个语句:System.out.printf(format,row [i]); only '|' 只有'|' and after that value of row[i] are getting printed so why are '%- (value of maxW[i]) and 's' not getting printed ? 在行[i]的值被打印之后,为什么'% - (maxW [i]的值)和's'没有打印?

Java format specifiers are described here . 这里描述 Java格式说明符。

This code is dynamically creating a format specifier that will left-justify a string (row[i]) with a minimum width specified by maxW[i]. 此代码动态创建一个格式说明符,它将左对齐一个字符串(row [i]),其最小宽度由maxW [i]指定。

Format specifiers follow the pattern on the first line below. 格式说明符遵循下面第一行的模式。 Underneath I've aligned the code above, showing where it fits into the pattern. 在下面我已经对齐上面的代码,显示它适合模式的位置。

    %[argument_index$][flags] [width] [.precision]conversion
" | %                  -     "+maxW[i]           +"s";

The format method assumes the String decribes a format as it states in the documentation. 格式方法假定String描述了文档中所述的格式。 It handles %x or the like a special way replacing it with the first, second etc argument. 它处理%x或类似的方式,用第一个,第二个等参数替换它。

If you want to know more I suggest you read the Javadoc for this method. 如果您想了解更多,我建议您阅读此方法的Javadoc。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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