java字符串饭转,集合遍历

1:指定字符串反转

package com.qa.dm.operation;

import com.qa.method.MethodData;

import java.io.IOException;

public class test005 {
    public static void main(String[] args) throws IOException, InterruptedException {
        String msg = "I love Beijing!";
        String[] split = msg.split(" ");
        for(int i =0; i<split.length;i++){
            if(i==1){
                String str = new StringBuffer(split[i]).reverse().toString();
                split[i] = str;
            }if (i == 2){
                String str1 = new StringBuffer(split[i]).reverse().toString();
                split[i] = str1;
            }
        }
        for (String s : split){
            System.out.print(s+"\t");
        }

    }
}

2:遍历Map集合

package com.qa.dm.operation;

import com.qa.method.MethodData;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class test005 {
    public static void main(String[] args) throws IOException, InterruptedException {
        Map<Integer,String> map = new HashMap<Integer, String>();
        map.put(1,"李晨");
        map.put(2,"李凯");
        map.put(3,"老黄");
        //第一种遍历的方法
        for(Integer in : map.keySet()){
            //map.keySet相当于取出了每一个key值
            String s = map.get(in);
            System.out.println(in+" "+s);
        }
        //第二种遍历方法使用迭代器
        Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();
        while (it.hasNext()){
            Map.Entry<Integer, String> entry = it.next();
            System.out.println(entry.getKey()+" "+entry.getValue());

        }

        //第三种遍历方法,推荐方法,处理容量大的时候使用
        for(Map.Entry<Integer,String> entry : map.entrySet()){
            System.out.println(entry.getKey()+" "+entry.getValue());

        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_38023156/article/details/89447825