0

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]?

4
  • Arrays can't be resized in C++, Use vectors instead and pushback when the array element meets your criterion Commented Jan 25, 2016 at 2:09
  • Sorry, forgot to mention this is written in Java Commented Jan 25, 2016 at 2:10
  • 1
    @Jeriel In Java, you would use an ArrayList and call add() instead of an fixed sized array. Commented Jan 25, 2016 at 2:12
  • Initialize a counter i outside the loop to zero, whenever an element meets criterion assign the element to array2[i] and increase i by 1. Commented Jan 25, 2016 at 2:12

2 Answers 2

2

Use an index variable to use with your new array. Create it with value 0 and increase it whenever you add a value to your array.

For the array size problem, I would use ArrayList. You can just add elements to it as it grows dynamically. This even allows you to add without needing the aforementioned index variable.

List<Integer> list = new ArrayList<>();
for (int value : array1) {
   if (value meets criterion) {
      list.add(value);
   }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can do this by setting array2 to be the same size as array1, that way in the best case, everything from array1 will be contained in array2.

int[] array1 = {1,2,3,4,5};
int[] array2 = new int[array1.length];

int j = 0;

for (int value : array1) {
    if (approved(value)) {
        array2[j++] = value;
    }
}

This can be bad if the arrays are large enough and very few elements of array1 actually meet your criterion. In that case, it will be better to have the memory grow as needed to fit the elements. You can use an ArrayList for this:

List<Integer> array2 = new ArrayList<>();
for (int value : array1) {
    if (approved(value)) {
        array2.add(value);
    }
}

4 Comments

Creating an array of same size is poor management of memory. The second option: No need to create a list of array1, you can use your foreach loop with arrays.
@Bifz In the best case, using an array of the same size is better because it will end up using less memory than an ArrayList. It will also be more efficient because it does not need to re-allocate memory frequently. Admittedly in the worst case, it will use more memory, but you can easily create one that has the required size and transfer everything to that one and let garbage collection deal with the other one
Thanks! I took a portion of this answer.What I've done is create one for loop that checks for how many elements from array1 meets the criterion - if so, b's array length++ and I will initialize b with that final array length. After which, I will iterate through a and assign the elements through b[j++] and a[i].
@Smac89 Consider your original array is huge and only a few of the numbers meet the criteria. How is that efficient memory management? If you really want to use an array, than you should just leave it as it is, computing the needed size first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.