1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JAVA参照渡し(引数としてアドレスを渡す)-配列

Last updated at Posted at 2015-10-15

▪️例

public class test38 {
	public static void incArray(int[] array) {
		for (int i = 0; i < array.length; i++) {
			array[i]++;
		}
	}

	public static void main(String[] args) {
		int[] array = { 1, 2, 3 };
		incArray(array);
		for (int i : array) {
			System.out.println(i);
		}
	}

}

▪️実行結果
2
3
4

▪️補足
もしarray[i]++;がなかったら、結果が
1
2
3
となる。

▪️例② 戻り値が配列の場合

public class test38 {
	public static int[] makeArray(int size) {
		int[] newArray = new int[size];
		for (int i = 0; i < newArray.length; i++) {
			newArray[i] = i;
		}
		return newArray;
	}

	public static void main(String[] args) {
		int[] array = makeArray(3);
		for (int i : array) {
			System.out.println(i);
		}
	}
}

▪️例② 実行結果
0
1
2

▪️例② 補足
int[] array = makeArray(3);をint[] array = makeArray(5);としたら、
結果が
0
1
2
3
4
となる。

1
1
0

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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?