一定期間更新がないため広告を表示しています
配列
- 2006.08.08 Tuesday
- 変数
- 21:24
- comments(1)
- trackbacks(0)
- by javaget
次のようにして、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();
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)
- 2006.08.08 Tuesday
- 変数
- 21:23
- comments(1)
- trackbacks(0)
- by javaget
クラスメソッドの中で this は自分自身を示す特別な変数名となります。下記の例では、this は、Person クラスのインスタンス自身を示します。
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
}
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
}
定数(static final)
- 2006.08.08 Tuesday
- 変数
- 21:22
- comments(0)
- trackbacks(0)
- by javaget
変数を下記のように定義することにより、変更不可能な 定数 を表現することが可能となります。定数は通常大文字で記述するのが習慣です。
class Math {
public static final double PI = 3.14159265358979323846;
}
class Math {
public static final double PI = 3.14159265358979323846;
}
変数修飾子(public, ...)
- 2006.08.08 Tuesday
- 変数
- 21:22
- comments(0)
- trackbacks(0)
- by javaget
変数の修飾子には、public、protected、private、static、final、transient、volatile を指定することができます。
class クラス名 {
修飾子 int testVariable;
:
}
class クラス名 {
修飾子 int testVariable;
:
}
変数
- 2006.08.08 Tuesday
- 変数
- 21:22
- comments(0)
- trackbacks(0)
- by javaget
値を保存しておくメモリに名前をつけたものを 変数(variable)と呼びます。
class クラス名 {
型 変数名;
}
例えば、int 型の変数 a を定義するには次のようにします。
class クラス名 {
int a;
}
class クラス名 {
型 変数名;
}
例えば、int 型の変数 a を定義するには次のようにします。
class クラス名 {
int a;
}
- selected entries
-
- 配列 (08/08)
- 自分自身(this) (08/08)
- 定数(static final) (08/08)
- 変数修飾子(public, ...) (08/08)
- 変数 (08/08)
- categories
- archives
-
- August 2006 (88)
- profile
- search this site.
- mobile
- calendar
-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
- sponsored links