foreach,for 操纵数组比较

package com.web;

public class Test28 {
    
    
    public static void main(String[] args) {
    
    
        //for对操作的数组或集合中的数据进行修改操作
        String[] str1 = {
    
    "a", "b", "c", "d", "e"};
        for (int i = 0; i < str1.length; i++) {
    
    
            if (i == 1) {
    
    
                str1[1] = "f";
            }
            System.out.println("str1[" + str1[i] + "]");
        }
        System.out.println("=================");
        for (int i = 0; i < str1.length; i++) {
    
    
            if (i == 0) {
    
    
                continue;
            }
            System.out.println("str1[" + str1[i] + "]");
        }
        System.out.println("=================");
        //foreach循环它仅仅只能用来完成遍历,不能对操作的数组或集合中的数据进行修改操作
        String[] str2 = {
    
    "a", "b", "c", "d", "e"};
        int count = 0;
        for (String s : str2) {
    
    
            if (s.equals("b")) {
    
    
             continue;
            }
            if (s=="e"){
    
    
                continue;
            }
            if (s.equals("c")){
    
    
                s="x";
            }
            System.out.println("str2[" + count + "] = " + s);
            count++;
        }
    }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/djydjy3333/article/details/121427076