0
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)

Posted at

ソースコード

ArrayTest.java
public class ArrayTest {
	public static void main(String[] args) {
		String[] array = {"あいうえお", "あいうえお", "かきくけこ", null, null};
		for (int i = 0; i < array.length-1; i++){
			if (array[i] == array[i+1]) {
				if (array[i] == null) {
					System.out.println(array[i]+" と "+array[i+1]+" は両方NULL");
				} else {
					System.out.println(array[i]+" と "+array[i+1]+" はおなじ");
				}
			} else {
				if (array[i] == null || array[i+1] == null) {
					System.out.println(array[i]+" と "+array[i+1]+" は片方NULL");
				} else {
					System.out.println(array[i]+" と "+array[i+1]+" はちがう");
				}
			}
		}
	}
}

出力結果

あいうえお と あいうえお はおなじ
あいうえお と かきくけこ はちがう
かきくけこ と null は片方NULL
null と null は両方NULL
0
1
3

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