简体   繁体   中英

Enhanced For loop Java

I am trying to write a for loop for the following integer array. The size of the array is 10. I wrote two loop. first loop will increase the value and the second loop will print the result like Element 1 is 10, Element 2 is 20. But I get 0 all the time. I gave previous code where everything wasfine. For testing reason I would like to implement enhanced for loop. But I stuck with 2 issues how can increment value and how can i define arraylength. Here is my code.

public static void main(String[] args) {

    int[] myIntArray = new int[10];

    for(Integer num: myIntArray){
        myIntArray[num]=num*10;
    }

    for(int num:myIntArray){
        System.out.println("Element " + num + ", value is " + myIntArray[num]);

    }
}

Previous Code

 int[] myIntArray =new int[10];

    for(int i =0; i<myIntArray.length;i++){
        myIntArray[i]=i*10;
    }
    for(int i=0;i<myIntArray.length;i++){
        System.out.println("Element " + i + ", value is " + myIntArray[i]);
    }

First you have to fill array with indexes:

    Arrays.setAll(myIntArray, i -> i);

And after that, during output you should not try to get value as myIntArray[num] , because using enhanced for loop you already have a value, but don't have its index. So the result code should be something like this:

public static void main(String[] args) {
    int[] myIntArray = new int[10];

    Arrays.setAll(myIntArray, i -> i);

    for(Integer num: myIntArray){
        myIntArray[num]=num*10;
    }

    for(int num : myIntArray){
        System.out.println("Element " + num/10 + ", value is " + num);

    }
}

Though I agree with previous commenters that enhanced loop is not the right choice to work with array indices.

They all return zero because when you construct the array, the default for each int in the array is zero. Then you multiply it by ten. Which will still return as zero. You need to populate the array before you use the for loop.

EDIT:

After seeing your edits I suggest not using an enhanced for loop for this. An enhanced for loop is a good tool when used in the correct situations. It is not automatically better than a regular for loop, and deciding which one to use should depend on the situation.

int[] myIntArray =new int[10]; you are instantiating an array of 10 elements and all of those elements are 0.

that's why here :

for(Integer num: myIntArray){
    myIntArray[num]=num*10;
}

you are doing more like 0*10 which is 0. To correct that :

for(int i=0; i<myIntArray.length;i++){
    myIntArray[i]=i*10;
}

and to display :

for(int num:myIntArray) {
    System.out.println("Element " + num + ", value is " + num);
}

That's simple math. 0*10 = 0 ;)

Your int array is not initialized and the default value for the int primitive is 0. If you want to preset it with values based on index you need to iterate on the array using an index or add one to the loop.

int index=0;
for(Integer num: myIntArray){
    myIntArray[index]=(++index)*10;

}

or

for(int index=0;index<myIntArray.length;i++){
    myIntArray[i]=(i+1)*10;
}

For each will hide the index variable in case if the object being looped over is an array. In case if the object being looped over is of type iterable, then the for each loop's implementation hides the iterator. So if its an array, and an index is required inside the loop, then better use a loop with array's indexes.

So the for each written this way:

for(Integer num: myIntArray){
  myIntArray[num]=num*10;
}

Is similar to below , in ur case :

for(int i =0; i<myIntArray.length;i++){
        myIntArray[i]=myIntArray[i]*10;
} 

And do not use the value in the array to index in side for each, as it will throw an index out of bounds exception if the value happens to be >= array length. Here it didn't as the value in the array is type int and they are all initialized to 0 which is less than array length, 10.

for(int num:myIntArray){
    System.out.println("Element " + num + ", value is " + myIntArray[num]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM