5

I can read data from an array with for each syntax:

int[] a = new int[100];
for (int i = 0; i < 100; i++){
    a[i] = i;
}


for (int element : a){
    System.out.println(element);                         
}

But is it possible to populate the array likewise. Say, with i*2 values?
I failed to invent such a method and would rather ask you whether I'm wrong or not.

Maroun
  • 94,125
  • 30
  • 188
  • 241
Kifsif
  • 3,477
  • 10
  • 36
  • 45

6 Answers6

7

From Java Docs,

The for-each loop hides the iterator, so you cannot call remove. Therefore, the for-eachloop is not usable for filtering. Similarly it is not usable for loops where you need to replace elements in a list or array as you traverse it. Finally, it is not usable for loops that must iterate over multiple collections in parallel. These shortcomings were known by the designers, who made a conscious decision to go with a clean, simple construct that wouldcover the great majority of cases.

So, in simple words, its not possible to populate arrays.

Maroun
  • 94,125
  • 30
  • 188
  • 241
Vikas V
  • 3,176
  • 2
  • 37
  • 60
3

It's unclear what you're trying to achieve. An enhanced for loop can only iterate over a collection - in your case, you don't have useful values in a collection to start with - only a collection you're trying to populate.

If you're just trying to populate one array based on some calculation which isn't based on an existing collection, an enhanced for loop isn't helpful.

Even if you do want to populate the array based on another collection, using an enhanced for loop isn't ideal, as you don't have any concept of an index. For example, if you have a String array and you want to populate an int array with the lengths of the strings, you can do:

String[] words = ...; // Populate the array
int[] lengths = new int[words.length];

int index = 0;
for (String word : words) {
    lengths[index++] = word.length();
}

... but it's not ideal. It's better if you're populating a List of course, as then you can just call add:

String[] words = ...; // Populate the array
List<Integer> lengths = new ArrayList<Integer>(words.length);

for (String word : words) {
    lengths.add(word.length());
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

No it is not possible. You'll have to use your first version of the for.

Burkhard
  • 14,596
  • 22
  • 87
  • 108
1

There is no counter for the current index, just the value at the index inthe foreach loop hence it is not possible.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

It is impossible to assign values to the actual array you are using in an enhanced for each loop. This is because an enhanced for-each loop does not give you access to the array's pointers. In order to change the values of the array, you must literally say:

a[i] = anything

You can however, use an enhanced for-loop to assign values to another array like so:

int[] nums = new int[4];
int[] setNums = {0,1,2,3};
i = 0;
for(int e: setNums) {
   nums[i++] = e*2;
}

Enhanced for-loops in Java merely offer a little syntactic sugar to get the values of the an array or List. In some ways, they operate similarly to passing Objects to a method - the original Object passed to the method cannot be re-assigned in the method. For example:

int i = 1;
void multiplyAndPrint(int p) {
  p = p*2;
  System.out.println(p);
}
System.out.println(i);

Will print 2 and then 1. This is the same issue you will run into attempting to assign values from a for-each loop.

Danieth
  • 405
  • 4
  • 6
1

To do that, you would need a way to get the index of 'element' in your array.

How to find the index of an element in an array in Java? has a lot of suggestions on how to do that.

Community
  • 1
  • 1
lyio
  • 1,237
  • 12
  • 13