MyClass[] a; a = new MyClass[10]; または MyClass[] a = new MyClass[10]; a[0] = new MyClass(); a[1] = new MyClass(); : a[9] = new MyClass(); a.length = 10 <-- 配列aの要素数 |
図.クラス型の配列 |
- public class Array02 {
- public static void main( String[] args ) {
- Sample[] classarray;
- classarray = new Sample[3];
- for ( int i = 0; i < classarray.length; i++ ) {
- classarray[ i ] = new Sample();
- classarray[ i ].aaa = i * 10;
- System.out.println( "classarray[" + i
- + "].aaa" + " = "
- + classarray[ i ].aaa );
- }
- }
- }
- public class Sample {
- int aaa = 0;
- }
□ 実行結果
$java Array02 classarray[0].aaa = 0 classarray[1].aaa = 10 classarray[2].aaa = 20
- public class EvenOdd03 {
- public static void main(String[] args) {
- int a = Integer.parseInt(args[0]);
- // ここにコードを記述してください。
- }
- }
□ 実行結果
$ java EvenOdd03 10 10は偶数です。 $ java EvenOdd03 11 11は奇数です。
- public class JavaIdentifier03 {
- public static boolean isJavaIdentifier(String s) {
- String[] keywords = {
- "abstract", "assert", "boolean", "break",
- "byte", "case", "catch", "char",
- "class", "const", "continue", "default",
- "do", "double", "else", "extends",
- "false", "final", "finally", "float",
- "for", "goto", "if", "implements",
- "import", "instanceof", "int", "interface",
- "long", "native", "new", "null",
- "package", "private", "protected", "public",
- "return", "short", "static", "strictfp",
- "super", "switch", "synchronized", "this",
- "throw", "throws", "transient ", "true",
- "try", "void", "volatile", "while"
- };
- if (!Character.isJavaIdentifierStart(s.charAt(0))) {
- return false;
- }
- for (int i = 1; i < s.length(); i++) {
- if (!Character.isJavaIdentifierPart(s.charAt(i))) {
- return false;
- }
- }
- // ここにコードを追加してください。
- }
- return true;
- }
- public static void main(String[] args) {
- if (isJavaIdentifier(args[0])) {
- System.out.println(args[0] + " is Java Identifier.");
- } else {
- System.out.println(args[0] + " is not Java Identifier.");
- }
- }
- }
□ 実行結果
$ java JavaIdentifier03 True True is Java Identifier. $ java JavaIdentifier03 true true is not Java Identifier.
Animal[] c = {new Cat(), new Dog(), new Ferret()}; (*) Catクラス, Dogクラス, Ferretクラスは、Animalインターフェースを実装しているものとします。 |