I'm trying to take certain elements from one array if they meet a specific criterion and assign them to another array in order. For example:
int[] array1 = {1,2,3,4,5}
int[] array2 = {}
(In terms of array2's length, I have created a separate for loop that iterates through array1 - if the element in array1 meets the criterion, array2.length++. Then I will initialize array2 one array1 has been fully iterated through.)
Let's say only array1[1],[3],[4] meet the criterion, so I want them to be transferred to array2.
So far I've done this:
for (iterate through array1) {
if (array1[i] meets criterion) {
a[i] = b[i];
}
}
I wanted to do a[i] = b[i], but if the index is, say, 4, b[] doesn't have an index 4. What can I do to make sure that array[1],[3],[4] will be assigned to array2 in order - array[1] = array2[0], array1[3] = array2[1], array1[4] = array2[2]?
ioutside the loop to zero, whenever an element meets criterion assign the element toarray2[i]and increase i by 1.