Java 软件测试编程题

1、计算给定字符串大小写字母出现次数,并打印。

public static void main(String[] args) {
        String str1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        
        int[] count = new int[52]; //用来存储字母a-z A-Z出现的次数。
         
        for(int i=0; i<str1.length(); i++){      
            char tmp = str1.charAt(i); //依次取出每个字母
            if(tmp>=65 && tmp<=90) {
                int index = tmp - 65;
                count[index] = count[index] +1;
            }else if(tmp>=97&& tmp<=122) {
                int index = tmp - 65+(7-1);
                count[index] = count[index] +1;
            }
        }
        
        //循环打印每个字母出现次数
        for(int j=0; j<count.length; j++){
            if(count[j]!=0) {
                char tmp = str1.charAt(j);
                if(tmp>=65 && tmp<=90) {
                    System.out.println("字母"+(char)(j+65)+"出现次数:"+count[j]);}
                else if(tmp>=97&& tmp<=122) {
                    System.out.println("字母"+(char)(j+65+(7-1))+"出现次数:"+count[j]);  // 7是 ascii表大小写字母中间的其他字符
                }
            }
        }
    }

2、利用map 实现统计给定字符串中大小写字母及数字出现次数。

String str = "ADHfggegJKEFdjiwDdgrgrgfGgrgrRd123";
			//用map实现
			TreeMap<Character,Integer> map = new TreeMap<Character,Integer>();
			for(Character ch: str.toCharArray()){
				
				//判断是否为字母,其他符号不考虑统计
				if( (ch>='a' && ch<='z') || (ch>='A'&&ch<='Z')){
					Integer count = map.get(ch);
					map.put(ch, null==count?1:count+1);
				}else if(ch >= '0' && ch <= '9') {
					Integer count = map.get(ch);
					map.put(ch, null==count?1:count+1);
				}
			}
			//遍历map
			for(Map.Entry<Character, Integer> enter: map.entrySet()){
				
				System.out.println("字母:"+enter.getKey() +"出现次数:"+enter.getValue());
				
			}

遍历map 的其他方法:

(1)利用迭代器和KeySet集合

		 Set<Character> keySet = map.keySet();
		        //遍历存放所有key的Set集合
		        Iterator<Character> it =keySet.iterator();    
		        while(it.hasNext()){                         //利用了Iterator迭代器**
		            //得到每一个key
		        	Character key = it.next();
		            //通过key获取对应的value
		        	Integer value = map.get(key);
		            System.out.println(key+"="+value);
		        }
			

(2)利用迭代器和map的entrySet集合

			Iterator<Map.Entry<Character,Integer>> iter = map.entrySet().iterator();
			 
			Map.Entry<Character,Integer> entry;
			 
			while (iter.hasNext()) {
			 
			    entry = iter.next();
			 
			    Character key = entry.getKey();
			 
			    Integer value = entry.getValue();
			    System.out.println("key = "+ key + ",vlaue = "+ value);
			 
			}

总结:for-each 效率较高,推荐使用。

 

3、比较两个数组的元素是否相等

使用数组的containsAll()方法


		//新建2个list,用来存储元素		
		List<String> A = Arrays.asList("Tom","Anthony","Beijing");
		List<String> B = Arrays.asList("Tom","Anthony","Beijing");
		
		if(A.containsAll(B)){
			System.out.println("相同");
		}else
			System.out.println("不相同");

这里补充说明下,定义不同类型的List 的格式:

注意:集合中只能存储引用数据类型,存储基本类型时,存储的类型为对应每个基本类型对应的引用数据。

int              Integer

double      Double

char          Character

String      String

List<Integer> inter= new ArrayList<Integer>();
List<Character> char1= new ArrayList<Character>();
List<Double> dou= new ArrayList<Double>();

 

----------------------------

边联系边记录

 

 

 

 

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值