スポンサーサイト

  • 2013.11.08 Friday
  • -
  • -
  • -
  • by スポンサードリンク

一定期間更新がないため広告を表示しています


配列

次のようにして、3個の要素からなる配列を生成することができます。

class Test {
public static void main(String[] args) {
int[] xx = new int[3];
xx[0] = 100;
xx[1] = 200;
xx[2] = 300;
}
}

次のようにして、生成と初期化を一度に行うこともできます。

int[] xx = { 100, 200, 300 };

配列の個数は length で参照できます。

int[] xx = new int[3];
int n = xx.length;

二次元の配列を生成することも可能です。

int[][] xx = new int[3][5];
String[][] ss = {
{ "Sun", "Sunday" },
{ "Mon", "Monday" }
};

オブジェクトの配列を生成するには、配列生成後、それぞれの要素も個別に生成する必要があります。

ClassA[] xx = new ClassA[3];
xx[0] = new ClassA();
xx[1] = new ClassA();
xx[2] = new ClassA();


自分自身(this)

クラスメソッドの中で this は自分自身を示す特別な変数名となります。下記の例では、this は、Person クラスのインスタンス自身を示します。

class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
}


定数(static final)

変数を下記のように定義することにより、変更不可能な 定数 を表現することが可能となります。定数は通常大文字で記述するのが習慣です。

class Math {
public static final double PI = 3.14159265358979323846;
}


変数修飾子(public, ...)

変数の修飾子には、public、protected、private、static、final、transient、volatile を指定することができます。

class クラス名 {
修飾子 int testVariable;
:
}


変数

値を保存しておくメモリに名前をつけたものを 変数(variable)と呼びます。

class クラス名 {
型 変数名;
}

例えば、int 型の変数 a を定義するには次のようにします。

class クラス名 {
int a;
}


相互リンク
selected entries
categories
archives
profile
search this site.
others
mobile
qrcode
powered
無料ブログ作成サービス JUGEM
calendar
     12
3456789
10111213141516
17181920212223
24252627282930
31      
<< March 2024 >>
sponsored links