【发布时间】:2014-03-06 09:29:21
【问题描述】:
我想生成单个字符串以用作默认标签——想想电子表格应用程序中的列标签。在我的特殊情况下,我只需要从 ["A".."Z"] 中的字符串集合中提取,但我尝试的解决方案可以应用于小写 Latin1 字母、数字、希腊字母表中的字符等。
Java 中的一个常见解决方案是这样的:
static final String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static public char getLabel( int index )
{
return alphabet.charAt( index );
}
它相当有效,如果index 不在 [0..25] 中,则会导致运行时错误,但它是典型的 Java,因为它很冗长,需要更多代码来验证它是否正常工作 - - 忘记了“U”还是用“V”转置了等等...
所以,相反,我查看了Character 类,看它是否提供了检索序数值(或 Unicode 索引或数值)的方法,这些值可以为诸如 'A' 之类的起始字母返回然后可以将 Unicode 块和类别添加到整数中,生成用于“查找”所需字符的结果,该结果使用另一种方法返回 char,给定有效的整数值,其中“有效”取决于 Unicode 编码。果然,有一些方法可以做到这一点,而且还有更多。事实上,似乎有几种方法可以做同样的事情,其中一些方法可以通过额外的选项来做同样的事情,例如为数值指定一个基数,然后在尝试理解“代码点”之间的差异时花费大量时间。 “数值”、“数字”(比方法名称所暗示的更复杂)等。简而言之,Character 似乎为我的简单要求提供了有用的方法,但包装在一个复杂得多的包中想清楚我需要做什么。
最后,可以选择对char 原语执行算术运算。比如:
assert 'B' == 'A' + 1;
嗯,差不多。 Java 通过在计算结果之前将所有小于int 的“整数”类型转换为int 来使事情复杂化。因为char 被认为是整数类型——并且是语言中唯一的无符号整数——所以即使Character 不是Number 和其他问题,它也会进行相同的转换。尽管如此,在必要时进行一些边界检查和强制转换,'A' + x 似乎非常方便,尽管它存在以下示例中指出的问题:
class CharTest
{
static final String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static public char getLabel( int index )
{
return alphabet.charAt( index );
}
public static void main( String[] argv )
{
System.out.print( "getLabel( 5 ): " );
System.out.println( getLabel( 5 ) );
char a = 'A';
char b = 'B';
System.out.print( "a: " );
System.out.println( a );
System.out.print( "b: " );
System.out.println( b );
System.out.print( "++a: " );
System.out.println( ++a );
System.out.print( "--a: " );
System.out.println( --a );
System.out.print( "a++: " );
System.out.println( a++ );
System.out.print( "a--: " );
System.out.println( a-- );
System.out.print( "a += 1: " );
System.out.println( a += 1 );
System.out.print( "a -= 1: " );
System.out.println( a -= 1 );
System.out.print( "a += 5: " );
System.out.println( a += 5 );
System.out.print( "a -= 5: " );
System.out.println( a -= 5 );
System.out.print( "a + 1: " );
System.out.println( a + 1 );
System.out.print( "a - 1: " );
System.out.println( a - 1 );
System.out.print( "a + (char) 1: " );
System.out.println( a + (char) 1 );
System.out.print( "a - (char) 1: " );
System.out.println( a - (char) 1 );
System.out.print( "a + b: " );
System.out.println( a + b );
// The casts are just to show intent. I am aware Java will
// simply add two ints and silently downcast to short.
short z = (short) 1 + (short) 1;
System.out.print( "short z = (short) 1 + (short) 1: " );
System.out.println( z );
// The same, only different...except the compiler
// now requires the cast on the right-hand side
// of the assignment to z or it fails with the
// following error message:
//
// error: possible loss of precision
short x = 1;
short y = 1;
z = (short) ( x + y );
System.out.print( "z = (short) ( x + y ): " );
System.out.println( z );
// Demonstrate that a is still 'A'. The following tests
// produce results which indicate it is NUL ('\0') or
// something even stranger...
System.out.print( "a: " );
System.out.println( a );
// The following will not compile without the explicit
// casts on the right-hand side of the assignments.
// This results from converting a, b, and 5 to ints
// before adding them which produces an int. The error
// reported by the compiler is:
//
// error: possible loss of precision
char c = (char) ( a + 5 );
System.out.print( "char c = (char) ( a + 5 ): " );
System.out.println( c );
char d = (char) ( a + b - a );
System.out.print( "char d = (char) ( a + b - a ): " );
System.out.println( d );
}
}
示例代码产生:
getLabel( 5 ): F
a: A
b: B
++a: B
--a: A
a++: A
a--: B
a += 1: B
a -= 1: A
a += 5: F
a -= 5: A
a + 1: 66
a - 1: 64
a + (char) 1: 66
a - (char) 1: 64
a + b: 131
short z = (short) 1 + (short) 1: 2
z = (short) ( x + y ): 2
a: A
char c = (char) ( a + 5 ): F
char d = (char) ( a + b - a ): B
请注意,涉及使用二元运算符的chars 的表达式需要显式转换,而仅使用一元、值更新运算符的表达式则不需要。
是否有任何理由不使用char 算术来解决我当前的相当琐碎的问题,其中涉及查找已知在Unicode 中特定块内排序的字符?或者,我应该使用索引查找字符串常量、Character 类提供的方法,还是我在 JDK 中忽略的另一个更简单的类?
【问题讨论】:
-
为什么不直接使用
.charAt(index % 26)? -
.charAt(index % 26)很好,但我认为您的意思是:"ABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(index % 26),对吧?由于应用%并不能完全满足我当前的要求,但在其他情况下可以。
标签: java math unicode char character