English 中文(简体)
Java格式阵列(使用格式类别)
原标题:Java format array (using the Formatter class)

我们有这样的方法:

public String getCommandString(Object...args) {
    return this.code + " " + String.format(this.pattern, args);
}

<代码>该编码:int和this.pattern:String

这种方法通常被称为:

// cmd.code = 20
// cmd.pattern = "%1$s = %2$d"
String str = cmd.getCommandString("foo", 3);   // -> "20 foo = 3"

和其他说明模式(这是一个非常简单的基于文字的服务器-客户方案)

现在,模式有可能考虑到各种论点,例如,

// cmd.code = 20
// cmd.pattern = ???
String s1 = cmd.getCommandString("a", "b", "c");               // -> 20 a b c
String s2 = cmd.getCommandString("Hello", "world");            // -> 20 Hello world
String s3 = cmd.getCommandString("1", "2, "3", "4", "5", "6"); // -> 20 1 2 3 4 5 6

或许假设每一论点都是相同的(说明)? 或者,我是否必须超越人工描述的方法与格式? 更具体地说,我寻求一种通用的描述模式,以形成各种论点(同样类型)。 我记得在C做了这样的事情,但在贾瓦这样做是可能的?

最佳回答

如果我正确理解,你要求的功能就不存在。 The javadoc section from the Formatter category:

形式审查者可以三种方式援引论点:

如果格式的投机者包含一个论点指数,则采用具体的指数。 论点索引是一种错误的分类,表明论点清单中的立场。 第一种论点被“1美元”指为“2美元”等。 争论不止一次。 例如:

formatter.format("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s", "a", "b", "c", "d") // -> "d c b a d c b a"

相对指数化在格式光谱仪含有“斜线”时使用; (u003c) 旗帜导致重新使用原格式光谱仪的理由。 如果没有以前的论点,就会出现失踪的FormatArgumentException。

formatter.format("%s %s % "a b b b" // "c" and "d" are ignored because they are not referenced

当格式投机者既无论据索引,也无“带”;旗帜时,使用普通指数。 采用普通指数化的每一种格式的投机者,均被指定为独立于明确或相对指数化所使用的指数的争论清单。

formatter.format("%s %s %s %s", "a", "b", "c", "d") // -> "a b c d"

一种使用所有指数形式的形式,例如:

formatter.format("%2$s %s % "b a a b" // "c" and "d" are ignored because they are not referenced

论点的最大数目受 Java虚拟机械规格定义的 Java阵列的最大范围的限制。 如果争论指数与现有论点不相符,那么就会出现失踪的FormatArgument Exception。

如果提出的论点多于形式上的推论,则不予理.。

What you are asking for is a way to construct a pattern that will accept an arbitrary number of arguments and use tham all. The problem is that the Formatter class can only associate format specifiers with arguments either explicitly, relatively, or ordinarily. In each of these cases, the number of forma specifiers is fixed. There is no construct in which you can repeat or loop a format specifier. The relative technique looks promising, but there is no way to nest, or loop, it.

问题回答

使用

public String getCommandString(Object...args) {
    // Want to have a "-" if args are not given
    return this.code + " " + args != null && args.length > 0
            ? Arrays.stream(args).map(o -> o.toString()).collect(Collectors.joining(" + ")) : "-";
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签