• 解决java switch……case不能匹配字符串的问题


    java1.7已经支持了匹配字符串

    方案1.

     1 enum Animal {  
     2 
     3     dog,cat,bear;
     4 
     5      public static Animal getAnimal(String animal){  
     6 
     7        return valueOf(animal );  
     8 
     9     }   
    10 
    11 }
    12 
    13  
    14 
    15 public class Client {  
    16 
    17    public void caseAnimal(String animal){  
    18 
    19        switch(Animal.getAnimal(animal)){  
    20 
    21        case cat:  
    22 
    23            System.out.println("this is a cat");  
    24 
    25            break;  
    26 
    27        case dog:  
    28 
    29            System.out.println("this is a dog");  
    30 
    31            break;  
    32 
    33        case bear:  
    34 
    35            System.out.println("this is a bear");  
    36 
    37            break;  
    38 
    39        }  
    40 
    41    }
    42 
    43    
    44 
    45   public static void main(String[] args) {
    46 
    47       Client client = new Client();  
    48 
    49        client.caseAnimal("cat"); 
    50 
    51 } 
    52 
    53 } 

     方案2

     1 enum Seasons{
     2 
     3          SPRING,SUMMER,AUTUMN,WINTER
     4 
     5 }
     6 
     7 public class TestMultiInterface{
     8 
     9          public static void main(String[] args){
    10 
    11                    Seasons str=Seasons.SPRING;
    12 
    13                    switch(str.ordinal()){
    14 
    15                    case 0:
    16 
    17                    System.out.println(str.toString());
    18 
    19                    break;
    20 
    21                    case 1:
    22 
    23                    System.out.println(str.toString());
    24 
    25                    break;
    26 
    27                    default:
    28 
    29                    System.out.println("没找到");
    30 
    31                    }
    32 
    33          }
    34 
    35 }

    方案3

     1 public void switchCase() { 
     2 
     3   Map<String,Integer> map=new HashMap<String, Integer>(); 
     4 
     5   map.put("中国", 1); 
     6 
     7   map.put("美国", 2); 
     8 
     9   map.put("英国", 3); 
    10 
    11   map.put("法国", 4); 
    12 
    13    
    14 
    15   String str="中国"; 
    16 
    17   switch(map.get(str)) 
    18 
    19   {       
    20 
    21    case 1: 
    22 
    23         System.out.println("中国"); 
    24 
    25         break; 
    26 
    27     case 2: 
    28 
    29         System.out.println("美国"); 
    30 
    31        break; 
    32 
    33     case 3:
    34 
    35         System.out.println("英国");
    36 
    37        break;
    38 
    39     case 4: 
    40 
    41        System.out.println("法国"); 
    42 
    43        break;   
    44 
    45    default: 
    46 
    47     System.out.println("default"); 
    48 
    49   } 
    50 
    51  } 

    方案4

     1 String str1 = "aa";
     2 
     3         String[] str2 = {"bb", "aa", "cc", "aaa"};
     4 
     5         for (int i = 0; i < str2.length; ++i){
     6 
     7             switch (str1.compareTo(str2[i])) {
     8 
     9               case 0: System.out.println("OK");
    10 
    11                       break;
    12 
    13               default: System.out.println("Error");
    14 
    15                         break;
    16 
    17             }
    18 
    19         }

    方案5

     1 public class Client {
     2 
     3          public Client(String animal) {
     4 
     5                    //使用String的hasCode取得字符串的哈希码,此方法要保证哈希码不重复
     6 
     7                    switch (animal.hashCode()) {
     8 
     9                    case 98262:
    10 
    11                             System.out.println("this is a cat");
    12 
    13                             break;
    14 
    15                    case 99644:
    16 
    17                             System.out.println("this is a dog");
    18 
    19                             break;
    20 
    21                    case 3019700:
    22 
    23                             System.out.println("this is a bear");
    24 
    25                             break;
    26 
    27                    }
    28 
    29          }
    30 
    31  
    32 
    33          public static void main(String[] args) {
    34 
    35                    Client client = new Client("bear");
    36 
    37          }

     方案6  如果是在使用简单工厂设计模式,可以考虑利用java反射技术代替switch

     1 public static UserDao createUser(){
     2   Animal animal=null;
     3   try {
     4    iu=(Animal) Class.forName(Animal接口下子类的完整类名).newInstance();
     5   }catch (Exception e) {
     6    e.printStackTrace();
     7   }
     8   return animal;
     9 
    10  }

    以上方案,有的是从网上收集来的,有的是自己原创的,因为时间过长,已经不记得从哪个地方摘抄下来的,没有注明转载的地方,十分抱歉!

  • 相关阅读:
    hibernate关联关系
    数据结构之二叉树java实现
    队列java实现
    栈java实现
    原生JS结合cookie实现商品评分组件
    JAVA学习第一阶段(2)
    JAVA学习第一阶段(1)
    如何修改hosts并保存
    运行Apache时出现the requested operation has failed
    实现一元多项式
  • 原文地址:https://www.cnblogs.com/tfgzs/p/3481081.html
Copyright © 2020-2023  润新知