使用JAVA中的增强for循环向数组中的每个元素添加一个 [英] Adding one to each element in an array using an enhanced for loop in JAVA

查看:627
本文介绍了使用JAVA中的增强for循环向数组中的每个元素添加一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,我在解决这个问题时遇到了一些麻烦。目的是通过向每个元素添加一个并使用ENHANCED for循环打印它来转换类似 {1,2,3,4} 的int数组,因此它将看起来像这样 {2,3,4,5} 。这是我到目前为止所得到的:

  int myArr [] = {1,2,3,4}; 

for(int i:myArr){
i = + 1;
myWindow.writeOutLine(i);
}

非常确定不接近,我不确定如何存储新的数组中的值,然后转到下一个。

解决方案

如果必须使用增强型循环,可以使用单独的索引变量。

  int myArr [] = {1,2,3,4}; 
int count = 0;
for(int i:myArr){
myArr [count] = i + 1;
myWindow.writeOutLine(myArr [count]);
count ++;
}


hey guys i'm having some trouble working this out. The aim is to convert an int array like this {1, 2, 3, 4} by adding one to each element and printing it using an ENHANCED for loop, so it will look like this {2, 3, 4, 5}. This is what i got so far :

    int myArr[] = {1, 2, 3, 4};

    for (int i: myArr){
        i =+1;
        myWindow.writeOutLine(i);
    }

Pretty sure that is not close, i'm unsure how to store the new value in the array and go to next.

解决方案

you can use separate index variable if you have to use enhanced loop.

int myArr[] = {1, 2, 3, 4};
int count = 0 ;
for (int i: myArr){
     myArr[count] = i+1;
     myWindow.writeOutLine(myArr[count]);
     count++;
}

这篇关于使用JAVA中的增强for循环向数组中的每个元素添加一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆