「Java」int[]配列をコレクションListに変換するサンプル

2021年1月5日

書式
List<Integer> lstA = new ArrayList<Integer>();
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
import java.util.ArrayList;
import java.util.List;
public class IntSample {
//int型配列
protected static int cftA[] = { 121, 231, 341, 451 };
public static void main(String[] args) {
List<Integer> lstA = new ArrayList<Integer>();
// int配列の要素をリストに格納
for (int i = 0; i < cftA.length; i++) {
lstA.add(cftA[i]);
}
System.out.println("コレクションリストの要素下記");
for (Integer m : lstA) {
System.out.println(m);
}
}
}
package com.arkgame.study; import java.util.ArrayList; import java.util.List; public class IntSample { //int型配列 protected static int cftA[] = { 121, 231, 341, 451 }; public static void main(String[] args) { List<Integer> lstA = new ArrayList<Integer>(); // int配列の要素をリストに格納 for (int i = 0; i < cftA.length; i++) { lstA.add(cftA[i]); } System.out.println("コレクションリストの要素下記"); for (Integer m : lstA) { System.out.println(m); } } }
package com.arkgame.study;

import java.util.ArrayList;
import java.util.List;

public class IntSample {

      //int型配列
      protected static int cftA[] = { 121, 231, 341, 451 };

      public static void main(String[] args) {
            List<Integer> lstA = new ArrayList<Integer>();
            // int配列の要素をリストに格納
            for (int i = 0; i < cftA.length; i++) {
                  lstA.add(cftA[i]);
            }

            System.out.println("コレクションリストの要素下記");
            for (Integer m : lstA) {
                  System.out.println(m);
            }

      }

}

実行結果
コレクションリストの要素下記
121
231
341
451

Java

Posted by arkgame