JAva解惑(パズル16~20)
パズル16:ラインプリンタ
以上のプログラムはプラットフォームに関係なく、どのプラットフォームでもコンパイルエラーが発生します.
確実に必須でない限り、Unicoeエスケープ文字は使用しないでください.
変更後:
これはプラットフォームに関連しています.一部のプラットフォームでは、unixは2つの完全な行区切り記号を印刷します.windowsでは、できない可能性があります.
パズル18:文字列チーズ
何が印刷されますか??
0から127ですが、後ろの128個の数は65533です...why?
プラットフォームのデフォルト文字セットを使用したnew String(byte[])コンストラクション関数が原因です
パズル19:きれいな火花
コンパイルに失敗しました.ブロックコメント、行コメントの使用は推奨されません.
私のクラスは何ですか
実行結果:
/////////////.class(解釈:正規表現、各文字を"/"で置き換える)
chapter2/Main20.class
chapter2/Main20.class
(解釈:エラー、windowsのファイル区切り文字は反スラッシュ""で、代替文字列に現れる反スラッシュはその後の文字をエスケープし、単独の反スラッシュであれば無効です)
Exception in thread "main"java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(Unknown Source)
at java.util.regex.Matcher.appendReplacement(Unknown Source)
at java.util.regex.Matcher.replaceAll(Unknown Source)
at java.lang.String.replaceAll(Unknown Source)
chapter2\Main20.class
chapter2\Main20.class
chapter2\Main20.class
public class Main {
public static void main(String[] args) {
// Note; \u000A is Unicode representation of linefeed (LF)
char c = 0x000A;
System.out.println(c);
}
}
以上のプログラムはプラットフォームに関係なく、どのプラットフォームでもコンパイルエラーが発生します.
確実に必須でない限り、Unicoeエスケープ文字は使用しないでください.
変更後:
char c ='
';
System.out.println(c);
これはプラットフォームに関連しています.一部のプラットフォームでは、unixは2つの完全な行区切り記号を印刷します.windowsでは、できない可能性があります.
パズル18:文字列チーズ
public class Main {
public static void main(String[] args) throws UnsupportedEncodingException {
byte bytes[] = new byte[256];
for (int i = 0; i < 256; i++) {
bytes[i] = (byte) i;
}
String str = new String(bytes); //
for (int i = 0; i < str.length(); i++) {
System.out.print((int) str.charAt(i) + " ");
}
Charset c= Charset.defaultCharset(); //
System.out.println("
"+c.displayName()); //GBK
}
}
何が印刷されますか??
0から127ですが、後ろの128個の数は65533です...why?
プラットフォームのデフォルト文字セットを使用したnew String(byte[])コンストラクション関数が原因です
パズル19:きれいな火花
public class Main19 {
public static void main(String[] args) {
System.out.println(classify('n') + classify('+') + classify('2'));
}
private static String classify(char c) {
if ("0123456789".indexOf(c) >= 0)
return "number ";
if ("abcdefghijklmnopqrstuvwxyz".indexOf(c) >= 0)
return "letter ";
[b]/*
operators not supported yet
if ("+-*/&|!=".indexOf(c) >= 0)
return "operator ";
*/[/b]
return "unknown ";
}
}
コンパイルに失敗しました.ブロックコメント、行コメントの使用は推奨されません.
私のクラスは何ですか
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main20 {
public static void main(String[] args) {
System.out.println(Main20.class.getName().replaceAll(".", "/")+".class"); //
System.out.println(Main20.class.getName().replaceAll("\\.", "/")+".class"); //
System.out.println(Main20.class.getName().replaceAll(Pattern.quote("."),"/")+".class");
// [color=red]System.out.println(Main20.class.getName().replaceAll("\\.",File.separator)+".class"); [/color]
System.out.println(Main20.class.getName().replaceAll("\\.",Matcher.quoteReplacement(File.separator))+".class");
System.out.println(Main20.class.getName().replace(".",File.separator)+".class"); //replace(CharSequence,CharSequence),
System.out.println(Main20.class.getName().replace('.',File.separatorChar)+".class"); //replace(char,char)
}
}
実行結果:
/////////////.class(解釈:正規表現、各文字を"/"で置き換える)
chapter2/Main20.class
chapter2/Main20.class
(解釈:エラー、windowsのファイル区切り文字は反スラッシュ""で、代替文字列に現れる反スラッシュはその後の文字をエスケープし、単独の反スラッシュであれば無効です)
Exception in thread "main"java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(Unknown Source)
at java.util.regex.Matcher.appendReplacement(Unknown Source)
at java.util.regex.Matcher.replaceAll(Unknown Source)
at java.lang.String.replaceAll(Unknown Source)
chapter2\Main20.class
chapter2\Main20.class
chapter2\Main20.class