一次迭代一个数组2个项目

编程入门 行业动态 更新时间:2024-10-24 20:15:41
本文介绍了一次迭代一个数组2个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个类似 {a,b,c,d} 的字符串数组。每两个连续的项是成对的,我想迭代数组,并能够将两个字符串一起考虑。目前我正在考虑使用循环和计数来计算我在迭代中的位置,但有更简单/更好的方法吗?

I have a string array that looks something like {"a","b","c","d"}. Each two consecutive items are pairs and I want to iterate through the array and be able to consider the two strings together. Currently I'm thinking of having a loop and a count to work out where I am in the iteration but is there a simpler/better way?

推荐答案

for (int i=0 ; i<a.length ; i+=2) { String first = a[i]; String second = a[i+1]; ... }

如果数组由来电者提供,那么你可能想要处理a.length不均匀的情况。

If the array is provided by a caller, then you might want to handle the case where a.length is not even.

更多推荐

一次迭代一个数组2个项目

本文发布于:2023-11-28 22:44:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644241.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   迭代   项目

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!