forEach

一、shushuJava 实例 foreach循环使用

foreach语句是java5的新特征之一,在遍历数组、集合方面,foreach为开发人员提供了极大的方便。

foreach 语法格式如下:

for(元素类型t 元素变量x : 遍历对象obj){ 
     引用了x的java语句; 
} 

以下实例演示了 for 和 foreach循环使用:

public class Main {
    public static void main(String[] args) {
        int[] intary = { 1,2,3,4};
        forDisplay(intary);
        foreachDisplay(intary);
    }
    public static void forDisplay(int[] a){  
        System.out.println("使用 for 循环数组");
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println();
    }
    public static void foreachDisplay(int[] data){
        System.out.println("使用 foreach 循环数组");
        for (int a  : data) {
            System.out.print(a+ " ");
        }
    }
}

以上代码运行输出结果为:

ArrayList: [1, 2, 3, 4]
更新 ArrayList: 10 20 30 40 

二、JavaScript forEach() 方法

forEach() 方法用于调用数组的每个元素,并将元素传递给回调函数。

注意: forEach() 对于空数组是不会执行回调函数的。

<button onclick="numbers.forEach(myFunction)">点我</button>
<p id="demo"></p>
 
<script>
demoP = document.getElementById("demo");
var numbers = [4, 9, 16, 25];
 
function myFunction(item, index) {
    demoP.innerHTML = demoP.innerHTML + "index[" + index + "]: " + item + "<br>"; 
}
</script>

输出结果为 :

index[0]: 4
index[1]: 9
index[2]: 16
index[3]: 25

其他形式的语法格式:

// 箭头函数
forEach((element) => { /* … */ })
forEach((element, index) => { /* … */ })
forEach((element, index, array) => { /* … */ })

// 回调函数
forEach(callbackFn)
forEach(callbackFn, thisArg)

// 内联回调函数
forEach(function(element) { /* … */ })
forEach(function(element, index) { /* … */ })
forEach(function(element, index, array){ /* … */ })
forEach(function(element, index, array) { /* … */ }, thisArg)

 forEach() 本身是不支持的 continue 与 break 语句的,我们可以通过 some 和 every 来实现。 

 使用 return 语句实现 continue 关键字的效果:

1.continue:

var arr = [1, 2, 3, 4, 5];

arr.forEach(function (item) {
    if (item === 3) {
        return;
    }
    console.log(item);
});

 

var arr = [1, 2, 3, 4, 5];

arr.some(function (item) {
        if (item === 2) {
                return;  // 不能为 return false
        }
        console.log(item);
});

 

 

2.break:

var arr = [1, 2, 3, 4, 5];

arr.every(function (item) {
        console.log(item);
        return item !== 3;
});

 

 

09-09
### forEachJavaScript 中的使用 #### 基本概念 在 JavaScript 中,`forEach()` 方法专门用于对数组进行遍历。它需要一个函数作为参数,这个由用户定义但不由用户直接调用的函数被称为回调函数(callback),在 `forEach` 中,该回调函数会被调用多次,数组有几个元素,函数就调用几次。`forEach()` 通过回调函数将元素传递出来,元素信息以参数的形式传递进回调函数,可通过定义形参来获取元素信息 [^1]。 #### 回调函数参数 `forEach()` 的回调函数共有三个参数: 1. 当前被遍历的元素 2. 当前遍历的元素的索引 3. 当前正在被遍历的数组对象 示例代码如下: ```javascript let arr = ['孙悟空', '猪八戒', '沙和尚', '唐僧']; arr.forEach(function (element, index, array) { console.log('element=', element); console.log('index=', index); console.log('array=', array); }); ``` #### 注意事项 - `forEach()` 遍历的范围在第一次调用回调函数前就会确定,调用 `forEach` 后添加到数组中的项不会被回调函数访问到 [^3]。 示例代码: ```javascript var arr = [1, 2, 3, 4, 5]; let count = 0; arr.forEach(function (item, index, arr) { count++; if (item === 2) { arr.push(8); } console.log(item); }); console.log('count的值' + count); console.log(arr); ``` - `forEach()` 对于空数组是不会执行回调函数的 [^4]。 #### 适用场景 `forEach` 适用于需要对数组中的每个元素执行相同操作的场景,例如打印数组元素信息、对数组元素进行简单的计算等。但它没有返回值,若使用 `console.log` 打印其返回结果,会得到 `undefined` [^2]。 示例代码: ```javascript const nums = [1, 2, 3]; const result = nums.forEach((num, index) => { console.log(`第${index + 1}个数是:${num}`); }); console.log(result); // undefined ``` ### JSTL 里的 foreach 标签使用 #### 基本概念 JSTL 里的 `foreach` 是一个循环标签,类似于 Java 里的 `for` 循环,里面的变量相当于一个局部变量,除了 `foreach` 标签范围就无法使用 [^5]。 #### 相关属性 - `item`:表示一个集合。 - `var`:表示每次遍历的对象。 - `varStatus`:表示遍历的状态,包含以下属性: - `current`:当前这次迭代的(集合)项。 - `index`:当前这次迭代从 0 开始的迭代索引。 - `count`:当前这次迭代从 1 开始的迭代计数。 - `first`:用来标明当前这轮迭代是否为第一次迭代的标志。 - `last`:用来标明当前这轮迭代是否为最后一次迭代的标志。 - `begin`:返回 `begin` 属性值。 - `end`:返回 `end` 属性值。 - `step`:返回 `step` 属性值。 #### 适用场景 JSTL 的 `foreach` 标签适用于在 JSP 页面中对集合进行遍历,将集合中的数据展示在页面上。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汲海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值