For each loop: is it possible to fill an array?

I can read data from an array with 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 you can also populate the array. Say using the values ​​of i*2 ?
I was not able to come up with such a method and rather ask you if I am mistaken or not.

+4
source share
6 answers

From Java Docs ,

An iterator is hidden for each loop, so you cannot call remove. Therefore for each cycle cannot be used for filtering. by analogy, it cannot be used for loops where you need to replace elements in a list or array when you go through it. Finally, it cannot be used for loops that must iterate over multiple collections in parallel. These flaws were known to designers who decided to go with a clean, simple design that would cover the vast majority of cases.

So, in simple words, it is impossible to fill arrays.

+7
source

It is not clear what you are trying to achieve. The improved for loop can only iterate over the collection β€” in your case, you have no useful values ​​in the collection you are starting with β€” only the collection you are trying to populate.

If you are just trying to fill one array based on some calculation that is not based on an existing collection, an extended loop loop will not help.

Even if you want to populate an array based on another collection, using an extended loop for a loop is not ideal since you have no concept of index. For example, if you have a String array, and you want to fill an int array with long 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 perfect. This is better if you populate the List , of course, since 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()); } 
+3
source

No, It is Immpossible. You will need to use your first version for.

+2
source

There is no counter for the current index, just the value in the index in the foreach loop, so this is not possible.

+1
source

It is not possible to assign values ​​to the actual array that you are using in extended for each loop. This is because the extended for-each loop does not give you access to array pointers. To change the values ​​of an array, you should literally say:

 a[i] = anything 

However, you can use the extended for-loop to assign values ​​to another array as follows:

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

Advanced for-loops in Java simply offer some syntactic sugar to get array or list values. In a sense, they work similarly to passing objects to a method - the original object passed to the method cannot be reassigned in the method. For instance:

 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 problem that you will encounter trying to assign values ​​from the for-each loop.

+1
source

To do this, you need a way to get the "element" index in your array.

How to find the index of an element in an array in Java? There are many suggestions on how to do this.

+1
source

Source: https://habr.com/ru/post/1496386/


All Articles