0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Java 普通のfor文と拡張for文の比較】

Last updated at Posted at 2025-02-22

拡張for文とは

配列といった複数の要素を持っているものからすべての要素に含まれる値を順番に取り出して処理するために使われる

for (データ型 変数名 :配列名) {
    繰り返す処理
}    

通常のfor文との比較

//通常のfor文
String[] names = {"John","Kate","Bob"};
for (int i = 0; i < names.length; i++) {
    System.out.println(names[i]);
}    
//拡張for文
String[] names = {"John","Kate","Bob"};
for (String name:names) {
    System.out.println(name);
}    

★拡張for文のメリット★

①拡張for文を使用した方が記述内容がシンプルでコーディングもしやすく可読性に優れている
②要素外参照を防げる
③無限ループを防げる

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?