深入学习java源码之Character.toChars()与Character.codePointCount()
字符串与数组的转换
char[] data = {'a', 'b', 'd'};
String s = new String(data);
Java 中定义数组的语法有两种:
type arrayName[];
type[] arrayName;
type 为Java中的任意数据类型,包括基本类型和组合类型,arrayName为数组名,必须是一个合法的标识符,[ ] 指明该变量是一个数组类型变量。例如:
- int demoArray[];
- int[] demoArray;
这两种形式没有区别,使用效果完全一样
Java在定义数组时并不为数组元素分配内存,因此[ ]中无需指定数组元素的个数,即数组长度。而且对于如上定义的一个数组是不能访问它的任何元素的,我们必须要为它分配内存空间,这时要用到运算符new,其格式如下:
arrayName=new type[arraySize];
其中,arraySize 为数组的长度,type 为数组的类型。如:
- demoArray=new int[3];
为一个整型数组分配3个int 型整数所占据的内存空间。
你可以在声明数组的同时进行初始化(静态初始化),也可以在声明以后进行初始化(动态初始化)。例如:
// 静态初始化
// 静态初始化的同时就为数组元素分配空间并赋值
int intArray[] = {1,2,3,4};
int arr[]=new int[]{1,2,3};
String[] abc = new String[] { "abc", "acd", "add" };
String [] sqls = new String[sheet.getLastRowNum()];
condictionSql = "INSERT INTO "+templateTable+"("+paramkey.toString()+") VALUES( "+paramval.toString()+")";
}
sqls[i-1] = condictionSql;
// 动态初始化
float floatArray[] = new float[3];
floatArray[0] = 1.0f;
floatArray[1] = 132.63f;
floatArray[2] = 100F;
Student students[]=new Student[3];
students[0]=new Student("张三",10);
students[1]=new Student("李四",11);
students[2]=new Student("王五",12);
for(Student s:students){
System.out.println(s);
}
可以通过下标来引用数组:
arrayName[index];
与C、C++不同,Java对数组元素要进行越界检查以保证安全性。
每个数组都有一个length属性来指明它的长度,例如 intArray.length 指明数组 intArray 的长度。
Object[] pointX = null;
Object[] pointY = null;
String sql1 = "select t.* from place t";
List<IBean> points =query(sql1, covergeId);
pointX = new Object[points.size()];
pointY = new Object[points.size()];
for(int j=0;j<points.size();j++){
pointX[j]=points.get(j).get("X");
pointY[j]=points.get(j).get("Y");
}
Modifier and Type | Method and Description |
---|---|
static int | charCount(int codePoint) 确定代表指定字符(Unicode代码点)所需的 |
char | charValue() 返回此 |
static int | codePointAt(char[] a, int index) 返回 |
static int | codePointAt(char[] a, int index, int limit) 返回 |
static int | codePointAt(CharSequence seq, int index) 返回 |
static int | codePointBefore(char[] a, int index) 返回 |
static int | codePointBefore(char[] a, int index, int start) 返回 |
static int | codePointBefore(CharSequence seq, int index) 返回的给定索引前面的代码点 |
static int | codePointCount(char[] a, int offset, int count) 返回 |
static int | codePointCount(CharSequence seq, int beginIndex, int endIndex) 返回指定字符序列的文本范围内的Unicode代码点数。 |
static int | compare(char x, char y) 数值比较两个 |
int | compareTo(Character anotherCharacter) 数字比较两个 |
boolean | equals(Object obj) 将此对象与指定对象进行比较。 |
static char | forDigit(int digit, int radix) 确定指定基数中特定数字的字符表示。 |
static byte | getDirectionality(char ch) 返回给定字符的Unicode方向属性。 |
static byte | getDirectionality(int codePoint) 返回给定字符的Unicode方向性属性(Unicode代码点)。 |
static String | getName(int codePoint) 返回指定字符的Unicode名称 |
static int | getNumericValue(char ch) 返回指定的Unicode字符代表的 |
static int | getNumericValue(int codePoint) 返回 |
static int | getType(char ch) 返回一个值,表示一个字符的一般类别。 |
static int | getType(int codePoint) 返回一个值,表示一个字符的一般类别。 |
int | hashCode() 返回这个 |
static int | hashCode(char value) 返回一个 |
static char | highSurrogate(int codePoint) 返回主导替代(一个 high surrogate code unit所述的) surrogate pair表示在UTF-16编码指定的补充的字符(Unicode代码点)。 |
static boolean | isAlphabetic(int codePoint) 确定指定的字符(Unicode代码点)是否是字母表。 |
static boolean | isBmpCodePoint(int codePoint) 确定指定的字符(Unicode代码点)是否在 Basic Multilingual Plane (BMP)中 。 |
static boolean | isHighSurrogate(char ch) 确定给定的 |
static boolean | isLowerCase(char ch) 确定指定的字符是否是小写字符。 |
static boolean | isLowerCase(int codePoint) 确定指定的字符(Unicode代码点)是否是小写字符。 |
static boolean | isLowSurrogate(char ch) 确定给定的 |
static boolean | isMirrored(char ch) 根据Unicode规范确定字符是否镜像。 |
static boolean | isMirrored(int codePoint) 确定是否根据Unicode规范镜像指定的字符(Unicode代码点)。 |
static boolean | isSpace(char ch) 已弃用 替换为isWhitespace(char)。 |
static boolean | isSpaceChar(char ch) 确定指定的字符是否是Unicode空格字符。 |
static boolean | isSpaceChar(int codePoint) 确定指定字符(Unicode代码点)是否为Unicode空格字符。 |
static boolean | isSupplementaryCodePoint(int codePoint) 确定指定字符(Unicode代码点)是否在 supplementary character范围内。 |
static boolean | isSurrogate(char ch) 确定给定的 |
static boolean | isSurrogatePair(char high, char low) 确定指定的一对 |
static boolean | isUpperCase(char ch) 确定指定的字符是否为大写字符。 |
static boolean | isUpperCase(int codePoint) 确定指定的字符(Unicode代码点)是否为大写字符。 |
static boolean | isValidCodePoint(int codePoint) 确定指定的代码点是否有效 Unicode code point value 。 |
static char | lowSurrogate(int codePoint) 返回尾随替代(一个 low surrogate code unit所述的) surrogate pair表示在UTF-16编码指定的补充的字符(Unicode代码点)。 |
static int | offsetByCodePoints(char[] a, int start, int count, int index, int codePointOffset) 返回给定的 |
static int | offsetByCodePoints(CharSequence seq, int index, int codePointOffset) 返回给定的char序列中与 |
static char | reverseBytes(char ch) 返回通过反转指定的 char值中的字节顺序获得的值。 |
static char[] | toChars(int codePoint) 将指定的字符(Unicode代码点)转换为存储在 |
static int | toChars(int codePoint, char[] dst, int dstIndex) 将指定的字符(Unicode代码点)转换为其UTF-16表示形式。 |
static int | toCodePoint(char high, char low) 将指定的代理对转换为其补充代码点值。 |
String | toString() 返回 |
static String | toString(char c) 返回一个 |
static Character | valueOf(char c) 返回一个 表示指定的 char值的 Character实例。 |
java源码
package java.lang;
import java.util.Arrays;
import java.util.Map;
import java.util.HashMap;
import java.util.Locale;
public final
class Character implements java.io.Serializable, Comparable<Character> {
public static final int MIN_RADIX = 2;
public static final int MAX_RADIX = 36;
public static final char MIN_VALUE = '\u0000';
public static final char MAX_VALUE = '\uFFFF';
@SuppressWarnings("unchecked")
public static final Class<Character> TYPE = (Class<Character>) Class.getPrimitiveClass("char");
public static final byte UNASSIGNED = 0;
public static final byte UPPERCASE_LETTER = 1;
public static final byte LOWERCASE_LETTER = 2;
......
public static final byte DECIMAL_DIGIT_NUMBER = 9;
static final int ERROR = 0xFFFFFFFF;
public static final byte DIRECTIONALITY_UNDEFINED = -1;
public static final char MIN_HIGH_SURROGATE = '\uD800';
public static final char MIN_LOW_SURROGATE = '\uDC00';
public static final char MIN_SURROGATE = MIN_HIGH_SURROGATE;
public static final char MAX_SURROGATE = MAX_LOW_SURROGATE;
public static final int MIN_SUPPLEMENTARY_CODE_POINT = 0x010000;
private final char value;
private static final long serialVersionUID = 3786198910865385080L;
public Character(char value) {
this.value = value;
}
private static class CharacterCache {
private CharacterCache(){}
static final Character cache[] = new Character[127 + 1];
static {
for (int i = 0; i < cache.length; i++)
cache[i] = new Character((char)i);
}
}
public static Character valueOf(char c) {
if (c <= 127) { // must cache
return CharacterCache.cache[(int)c];
}
return new Character(c);
}
public char charValue() {
return value;
}
@Override
public int hashCode() {
return Character.hashCode(value);
}
public static int hashCode(char value) {
return (int)value;
}
public boolean equals(Object obj) {
if (obj instanceof Character) {
return value == ((Character)obj).charValue();
}
return false;
}
public String toString() {
char buf[] = {value};
return String.valueOf(buf);
}
public static String toString(char c) {
return String.valueOf(c);
}
public static boolean isValidCodePoint(int codePoint) {
// Optimized form of:
// codePoint >= MIN_CODE_POINT && codePoint <= MAX_CODE_POINT
int plane = codePoint >>> 16;
return plane < ((MAX_CODE_POINT + 1) >>> 16);
}
public static int compare(char x, char y) {
return x - y;
}
public int compareTo(Character anotherCharacter) {
return compare(this.value, anotherCharacter.value);
}
public static boolean isBmpCodePoint(int codePoint) {
return codePoint >>> 16 == 0;
// Optimized form of:
// codePoint >= MIN_VALUE && codePoint <= MAX_VALUE
// We consistently use logical shift (>>>) to facilitate
// additional runtime optimizations.
}
public static boolean isSupplementaryCodePoint(int codePoint) {
return codePoint >= MIN_SUPPLEMENTARY_CODE_POINT
&& codePoint < MAX_CODE_POINT + 1;
}
public static boolean isHighSurrogate(char ch) {
// Help VM constant-fold; MAX_HIGH_SURROGATE + 1 == MIN_LOW_SURROGATE
return ch >= MIN_HIGH_SURROGATE && ch < (MAX_HIGH_SURROGATE + 1);
}
public static boolean isLowSurrogate(char ch) {
return ch >= MIN_LOW_SURROGATE && ch < (MAX_LOW_SURROGATE + 1);
}
public static boolean isSurrogate(char ch) {
return ch >= MIN_SURROGATE && ch < (MAX_SURROGATE + 1);
}
public static boolean isSurrogatePair(char high, char low) {
return isHighSurrogate(high) && isLowSurrogate(low);
}
public static int charCount(int codePoint) {
return codePoint >= MIN_SUPPLEMENTARY_CODE_POINT ? 2 : 1;
}
public static int toCodePoint(char high, char low) {
// Optimized form of:
// return ((high - MIN_HIGH_SURROGATE) << 10)
// + (low - MIN_LOW_SURROGATE)
// + MIN_SUPPLEMENTARY_CODE_POINT;
return ((high << 10) + low) + (MIN_SUPPLEMENTARY_CODE_POINT
- (MIN_HIGH_SURROGATE << 10)
- MIN_LOW_SURROGATE);
}
public static int codePointAt(CharSequence seq, int index) {
char c1 = seq.charAt(index);
if (isHighSurrogate(c1) && ++index < seq.length()) {
char c2 = seq.charAt(index);
if (isLowSurrogate(c2)) {
return toCodePoint(c1, c2);
}
}
return c1;
}
public static int codePointAt(char[] a, int index) {
return codePointAtImpl(a, index, a.length);
}
public static int codePointAt(char[] a, int index, int limit) {
if (index >= limit || limit < 0 || limit > a.length) {
throw new IndexOutOfBoundsException();
}
return codePointAtImpl(a, index, limit);
}
static int codePointAtImpl(char[] a, int index, int limit) {
char c1 = a[index];
if (isHighSurrogate(c1) && ++index < limit) {
char c2 = a[index];
if (isLowSurrogate(c2)) {
return toCodePoint(c1, c2);
}
}
return c1;
}
public static int codePointBefore(CharSequence seq, int index) {
char c2 = seq.charAt(--index);
if (isLowSurrogate(c2) && index > 0) {
char c1 = seq.charAt(--index);
if (isHighSurrogate(c1)) {
return toCodePoint(c1, c2);
}
}
return c2;
}
public static int codePointBefore(char[] a, int index) {
return codePointBeforeImpl(a, index, 0);
}
public static int codePointBefore(char[] a, int index, int start) {
if (index <= start || start < 0 || start >= a.length) {
throw new IndexOutOfBoundsException();
}
return codePointBeforeImpl(a, index, start);
}
// throws ArrayIndexOutOfBoundsException if index-1 out of bounds
static int codePointBeforeImpl(char[] a, int index, int start) {
char c2 = a[--index];
if (isLowSurrogate(c2) && index > start) {
char c1 = a[--index];
if (isHighSurrogate(c1)) {
return toCodePoint(c1, c2);
}
}
return c2;
}
public static char highSurrogate(int codePoint) {
return (char) ((codePoint >>> 10)
+ (MIN_HIGH_SURROGATE - (MIN_SUPPLEMENTARY_CODE_POINT >>> 10)));
}
public static char lowSurrogate(int codePoint) {
return (char) ((codePoint & 0x3ff) + MIN_LOW_SURROGATE);
}
public static int toChars(int codePoint, char[] dst, int dstIndex) {
if (isBmpCodePoint(codePoint)) {
dst[dstIndex] = (char) codePoint;
return 1;
} else if (isValidCodePoint(codePoint)) {
toSurrogates(codePoint, dst, dstIndex);
return 2;
} else {
throw new IllegalArgumentException();
}
}
public static char[] toChars(int codePoint) {
if (isBmpCodePoint(codePoint)) {
return new char[] { (char) codePoint };
} else if (isValidCodePoint(codePoint)) {
char[] result = new char[2];
toSurrogates(codePoint, result, 0);
return result;
} else {
throw new IllegalArgumentException();
}
}
static void toSurrogates(int codePoint, char[] dst, int index) {
// We write elements "backwards" to guarantee all-or-nothing
dst[index+1] = lowSurrogate(codePoint);
dst[index] = highSurrogate(codePoint);
}
public static int codePointCount(CharSequence seq, int beginIndex, int endIndex) {
int length = seq.length();
if (beginIndex < 0 || endIndex > length || beginIndex > endIndex) {
throw new IndexOutOfBoundsException();
}
int n = endIndex - beginIndex;
for (int i = beginIndex; i < endIndex; ) {
if (isHighSurrogate(seq.charAt(i++)) && i < endIndex &&
isLowSurrogate(seq.charAt(i))) {
n--;
i++;
}
}
return n;
}
public static int codePointCount(char[] a, int offset, int count) {
if (count > a.length - offset || offset < 0 || count < 0) {
throw new IndexOutOfBoundsException();
}
return codePointCountImpl(a, offset, count);
}
static int codePointCountImpl(char[] a, int offset, int count) {
int endIndex = offset + count;
int n = count;
for (int i = offset; i < endIndex; ) {
if (isHighSurrogate(a[i++]) && i < endIndex &&
isLowSurrogate(a[i])) {
n--;
i++;
}
}
return n;
}
public static int offsetByCodePoints(CharSequence seq, int index,
int codePointOffset) {
int length = seq.length();
if (index < 0 || index > length) {
throw new IndexOutOfBoundsException();
}
int x = index;
if (codePointOffset >= 0) {
int i;
for (i = 0; x < length && i < codePointOffset; i++) {
if (isHighSurrogate(seq.charAt(x++)) && x < length &&
isLowSurrogate(seq.charAt(x))) {
x++;
}
}
if (i < codePointOffset) {
throw new IndexOutOfBoundsException();
}
} else {
int i;
for (i = codePointOffset; x > 0 && i < 0; i++) {
if (isLowSurrogate(seq.charAt(--x)) && x > 0 &&
isHighSurrogate(seq.charAt(x-1))) {
x--;
}
}
if (i < 0) {
throw new IndexOutOfBoundsException();
}
}
return x;
}
public static int offsetByCodePoints(char[] a, int start, int count,
int index, int codePointOffset) {
if (count > a.length-start || start < 0 || count < 0
|| index < start || index > start+count) {
throw new IndexOutOfBoundsException();
}
return offsetByCodePointsImpl(a, start, count, index, codePointOffset);
}
static int offsetByCodePointsImpl(char[]a, int start, int count,
int index, int codePointOffset) {
int x = index;
if (codePointOffset >= 0) {
int limit = start + count;
int i;
for (i = 0; x < limit && i < codePointOffset; i++) {
if (isHighSurrogate(a[x++]) && x < limit &&
isLowSurrogate(a[x])) {
x++;
}
}
if (i < codePointOffset) {
throw new IndexOutOfBoundsException();
}
} else {
int i;
for (i = codePointOffset; x > start && i < 0; i++) {
if (isLowSurrogate(a[--x]) && x > start &&
isHighSurrogate(a[x-1])) {
x--;
}
}
if (i < 0) {
throw new IndexOutOfBoundsException();
}
}
return x;
}
}