import java.util.ArrayList; import java.util.Collections; import java.util.List; public class CollectionsSortStringList { public static void main(String[] args){ CollectionsSortStringList cl = new CollectionsSortStringList(); cl.sortStringList(); } public void sortStringList(){ String str = "258398364358kjngdflbndfknSNRFONRFOLKNFOLNF"; List<String> list = new ArrayList<String>(); for(int j = 0;j < 10;j++) { String s = ""; for (int i = 0; i < (Math.random()*10); i++) { char k = str.charAt((int) (Math.random() * (str.length() - 1))); s = s + k; } list.add(s); } System.out.println("排序前------------------"); for (String l:list) { System.out.println(l); } System.out.println("排序后------------------"); Collections.sort(list); for (String l:list) { System.out.println(l); } } }
你这个好像不太符合题意呀,题目要求是随机生成的字符串,不是你自己定义的
还行。