4
3

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

Java入門
配列と繰り返し

前回はfor文の使い方についてやりました。
今回は配列とfor文の組み合わせです。

for文は繰り返しの回数を指定してあげる必要がありました。今回は自分で数を指定せずに、配列の要素数だけ繰り返すというfor文を書きます。C言語では存在していない機能ですね。

sample.java
public class Sample {
	public static void main (String[] args) {
		String[] message = {"ゆかりん", "世界一かわいいよ!", "Fooo!"};
		for (String value : message) {
			System.out.println(value);
		}
	}
}

これを実行すると

sample.java
ゆかりん
世界一かわいいよ!
Fooo!

このようになります。短くすっきりとしたfor文ですね。
今回は、文字列を扱うString型を使いましたが、もちろん整数のintなどでも可能です。
その際には、for文の中の型と一致させる必要があるので、そこを忘れないようにしてください。

4
3
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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?