List an Array of Strings in alphabetical order

前端 未结 11 1466
独厮守ぢ
独厮守ぢ 2020-12-28 14:49

I have a program which has the user inputs a list of names. I have a switch case going to a function which I would like to have the names print off in alphabetical order.

11条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-28 15:08

    CompareTo() method: The two strings are compared based on Unicode character values.

    import java.util.*;
    
    public class Test {
    
    int n,i,temp;
    String names[n];
    
    public static void main(String[] args) {
    String names[5] = {"Brian","Joshua","Louis","David","Marcus"};
    
    for(i=0;i<5;i++){
        for(j=i+1;i0) {
                 temp=names[i];
                 names[i]=names[j];
                 names[j]=temp;
             } else
                 System.out.println("Alphabetically Ordered");                               
             }
         }                              
    }
    

提交回复
热议问题