java字符串饭转,集合遍历

本文介绍了一种特殊的字符串反转方法,仅反转特定位置的字符,并详细展示了三种不同的Java Map遍历方式,包括直接遍历key、使用迭代器以及推荐的增强型for循环。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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());

        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值