五十道编程小题目 --- 40 字符串排序 java

【程序40】 

题目:字符串排序。 


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test40 {
	
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		ArrayList arrayList = new ArrayList<>();
		int i = 0;
		String str = "";
		System.out.println("请输出字符串(回车下一组,输入stop则停止输入):");
		while(s.hasNext()){
			str = s.nextLine();
			if(!str.equalsIgnoreCase("stop")){
				arrayList.add(str);
			}else{
				break;
			}
			
		}
		
		System.out.println("您输入的顺序为:");
		System.out.println(arrayList.toString());
		System.out.println("排序:");
		stringSort(arrayList);
		
	}

	private static void stringSort(ArrayList arrList) {

		ArrayList arrayList1 = new ArrayList<>();
		ArrayList arrayList2 = new ArrayList<>();
		
		//选择排序法
		for(int i=0; i 0 ){
					swap(arrList,i,j);
					
				}
			}
		}
		System.out.println(arrList.toString());
		
	}
	
	//数组交换位置
	private static void swap(ArrayList arr, int i , int j ){
		String strTmp = (String) arr.get(i);
		arr.set(i,arr.get(j) );
		arr.set(j, strTmp);
	}
	

}
输出结果:

请输出字符串(回车下一组,输入stop则停止输入):
dsfg
sgd
fhfhfrc
cfg
zsdwefc
abced
stop
您输入的顺序为:
[dsfg, sgd, fhfhfrc, cfg, zsdwefc, abced]
排序:
[abced, cfg, dsfg, fhfhfrc, sgd, zsdwefc]











你可能感兴趣的:(java进阶,五十道编程小算法,编程,算法)