【java】数组遍历的方式:


1、for循环遍历:

image.png

2、forEach循环(增强for循环):

image.png

3、while循环 或者 do while循环:

image.png

4、利用Arrays工具类当中的toString():
int [] array = {
    
    1,2,3,4,5};
System.out.print(Arrays.toString(array));
5、流式遍历:
int[] arr = {
    
    1, 2, 3, 4, 5};
Arrays.stream(arr).forEach(num -> System.out.println(num));
6、使用Arrays.asList()和forEach()方法进行遍历:
Integer[] arr = {
    
    1, 2, 3, 4, 5};
List<Integer> list = Arrays.asList(arr);
list.forEach(num -> System.out.println(num));

猜你喜欢

转载自blog.csdn.net/weixin_53791978/article/details/134955748