数组的创建及遍历
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; } }
本文来自博客园,作者:弈星,转载请注明原文链接:https://www.cnblogs.com/8023yyl/p/17099741.html