Created
November 14, 2011 09:19
-
-
Save tmd45/1363588 to your computer and use it in GitHub Desktop.
リストを好きな順番に並べ替えたい。要素数は可変。今回の場合は最初の3つだけ順番になってれば良いorz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package localtest; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Test6 { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
System.out.println("WANT TO SORT 'Apple', 'Bee', 'Elc', some... \r\n------------"); | |
List<String> list1 = new ArrayList<String>(); | |
list1.add("Corp"); | |
list1.add("Apple"); | |
list1.add("Dog"); | |
list1.add("Elc"); | |
list1.add("Bee"); | |
list1.add("Zen"); | |
List<String> list2 = new ArrayList<String>(); | |
int n = 0; | |
for(String str1 : list1) { | |
if("Apple".equals(str1)){ | |
list2.add(0, str1); | |
n++; | |
} | |
else if("Bee".equals(str1)) { | |
list2.add(n, str1); | |
n++; | |
} | |
else if("Elc".equals(str1)){ | |
list2.add(n, str1); | |
//n++; | |
} | |
else { | |
list2.add(str1); | |
} | |
} | |
for(String str1 : list1) { | |
int num = list1.indexOf(str1); | |
System.out.println("LIST1[" + num + "] >> " + str1); | |
} | |
System.out.println("------------ \r\nAFTER SORT \r\n------------"); | |
for(String str2 : list2) { | |
int num = list2.indexOf(str2); | |
System.out.println("LIST2[" + num + "] >> " + str2); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Elc ってなんぞ・・・