0
0

More than 1 year has passed since last update.

配列の勉強する

Posted at

1次元配列の定義、値変更、配列全体の参照渡し

sample.java
public class Mavenproject1 {
    int i1[];
    int i2[];
    public Mavenproject1() {
        i1 = new int[2];
        i2 = new int[2];        
    }
    public static void main(String[] args) {
        Mavenproject1 m = new Mavenproject1();
        m.i1[0] = 1;
        m.i1[1] = 1;
        m.i2[0] = 2;
        m.i2[1] = 2;
        m.putlog();
        m.i2 = m.i1;
        m.i1[1] = 3;
        m.putlog();
    }
    public void putlog(){
        System.out.println("i1[0] = " + i1[0]);
        System.out.println("i1[1] = " + i1[1]);
        System.out.println("i2[0] = " + i2[0]);
        System.out.println("i2[1] = " + i2[1]);
        System.out.println("------------------");
    }
}
i1[0] = 1
i1[1] = 1
i2[0] = 2
i2[1] = 2
------------------
i1[0] = 1
i1[1] = 3
i2[0] = 1
i2[1] = 3
------------------

配列初期化子

sample.java
    public static void main(String[] args) {
        int i1[] = {1,2,3};
        System.out.println("i1[0] = " + i1[0]);
        System.out.println("i1[1] = " + i1[1]);
        System.out.println("i1[2] = " + i1[2]);
    }
i1[0] = 1
i1[1] = 2
i1[2] = 3

2次元配列の定義

sample.java
    public static void main(String[] args) {
        int i1[][] = new int[2][2];
        i1[0][0] = 1;
        i1[0][1] = 2;
        i1[1][0] = 3;
        i1[1][1] = 4;
    }
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0