数组的创建及遍历

 

package com.test;
import java.util.*;

import static java.util.Arrays.fill;

public class Main {
    public static void main(String[] args) {
        int [] a = new int[10];//此处以int型为例
        for(int i = 0; i < 10; i ++){
            a[i] = 2 * i + 1 ;
        }
        for(int i = 0; i < 10; i ++){//用循环语句进行遍历(for while do-while)
            System.out.println(a[i]);
        }
        for(int i : a ){//用foreach语句进行遍历
            System.out.println(i);
        }
        Arrays.fill(a,5);//将数组中每个元素都修改为5
         System.out.println(Arrays.toString(a));//用Arrays类中的toString方法遍历,注意要写上import static java.util.Arrays.fill;
    }
}

 

posted @ 2023-02-07 20:30  弈星  阅读(17)  评论(0编辑  收藏  举报