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?

More than 3 years have passed since last update.

Javaの配列とリスト配列で配列の要素を数える方法が違います

Last updated at Posted at 2021-05-16

Javaで配列とリスト配列で配列の要素を数える方法が違います。

配列の場合は

public class Main{
   public static void main(String[] args){
       String[] data = {"test1","test2","test3"};
       for(int i=0;i<data.length;i++){
          System.out.println(data[i]);
       }
   }
}

に対し、リスト配列では

import java.util.*;

public class Main{
   public static void main(String[] args){

      ArrayList<String> data = new ArrayList<String>();
      data.add("test1");
      data.add("test2");
      data.add("test3");
      for (int i=0;i<data.size();i++){
          System.out.println(data.get(i));
      }
   }
}

になります。配列ではlengthプロパティに対し、リスト配列ではsize関数になります。
注)今回は配列がlengthプロパティでリスト配列がsize関数を使うのか混同する知り合いの方向けに作成したため、拡張for文については割愛させていただきます。

0
0
4

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?