public static void main(String[] args){ int[] sales; sales = new int[3]; sales[0] = 100; sales[1] = 200; sales[2] = 300; System.out.println(sales[1]); }
[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
200
こうも書ける
public static void main(String[] args){ int[] sales; sales = new int[]{200, 240, 280}; System.out.println(sales[1]); }
forと組み合わせる。
public static void main(String[] args){
int[] sales = {800, 540, 320};
for(int i = 0; i < 3; i++){ System.out.println(sales[i]); } } [/java] (int sale: sales)という書き方 [java] public static void main(String[] args){ int[] sales = {800, 540, 320}; for(int sale: sales){ System.out.println(sale); } } [/java] 基本データ型と参照型 [java] public static void main(String[] args){ int x = 10; int y = x; y = 5; System.out.println(x); System.out.println(y); } [/java] [java] int[] a = {3, 5, 7}; int[] b = a; b[1] = 8; System.out.println(a[1]); System.out.println(b[1]); [/java] 配列の場合はメモリ領域が入る。 [vagrant@localhost java]$ javac MyApp.java [vagrant@localhost java]$ java MyApp 8 8 文字列は別の領域に割り当てる。 [java] public static void main(String[] args){ String s = "hello"; String t = s; t = "world"; System.out.println(s); System.out.println(t); } [/java]