「Java」Collections.sort()メソッドで配列の要素を昇順にする

2020年11月10日

説明
public static <T extends Comparable<? super T>> void sort(List<T> list)
指定されたリストを、その要素の自然順序付けに従って昇順にソートします。リストのすべての要素は、
Comparableインタフェースを実装する必要があります。
Javaコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study.it;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class CompatrorDemo {
public static Integer[] cft = { 12, 35, 46, 9 };
public static void main(String[] args) {
List<Integer> cftLst = Arrays.asList(cft);
// 昇順
Collections.sort(cftLst);
// 要素表示
System.out.println("数値昇順の結果");
for (Integer tt : cftLst) {
System.out.println(tt);
}
}
}
package com.arkgame.study.it; import java.util.Arrays; import java.util.Collections; import java.util.List; public class CompatrorDemo { public static Integer[] cft = { 12, 35, 46, 9 }; public static void main(String[] args) { List<Integer> cftLst = Arrays.asList(cft); // 昇順 Collections.sort(cftLst); // 要素表示 System.out.println("数値昇順の結果"); for (Integer tt : cftLst) { System.out.println(tt); } } }
package com.arkgame.study.it;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CompatrorDemo {

      public static Integer[] cft = { 12, 35, 46, 9 };

      public static void main(String[] args) {
            List<Integer> cftLst = Arrays.asList(cft);
            // 昇順
            Collections.sort(cftLst);
            // 要素表示
            System.out.println("数値昇順の結果");
            for (Integer tt : cftLst) {
                  System.out.println(tt);

            }
      }

}

数値昇順の結果
9
12
35
46

Java

Posted by arkgame