Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Ene Catalin
Ene Catalin
1,931 Points

I need to copy the elements of "array" that can NOT be divided through 4, to "array2".

How can I copy certain elements from a biger array to a smaller array ? The for loop returns ArrayOutOfBoundsException error and I understand why, but I can't see any alternative.

And after I do that, "array2" should have the elements :{1, 15}. How cand I merge "array" and "array2" into a 2d array? ex.: int [][] array3 = {{1, 4, 0, 12, 15}, {1, 15}};

Thanks! Imgur

2 Answers

Emmanuel C
Emmanuel C
10,636 Points

Whats happening is that when you check if 15 divides into 4 and it doesnt, youre at the i = 4 iteration. so when it goes to assign the values array2[4] = array[4]; it errors out since array2 only have a length of 2. What i would do is use a list. List<int> array2List = new ArrayList<int>(); Then while you loop through array you can just use the list method add() to store them into your list. array2List.add(array[i]); At the end of the loop you can convert that list into an array by using the list.toarray() method. int[] array2 = new int[array2List.size()]; array2 = array2List.toArray(array2);

Hope this helps!

Ene Catalin
Ene Catalin
1,931 Points

That solved the problem, but my unversity won't allow ArrayLists for now. So I need a more basic solution

Emmanuel C
Emmanuel C
10,636 Points

You can use a separate counter, starts at 0 and gets incremented inside if statement.

Ene Catalin
Ene Catalin
1,931 Points

I can't really understand how you mean it

Emmanuel C
Emmanuel C
10,636 Points
int numsNotDivis = 0;

for(int i = 0; i < array.length; i++){
    if(array[i]  % div != 0){
        array2[numsNotDivis] = array[i];
        numsNotDiv++;
    }
}

Sorry for long variable name but counter was already taken. array2 having its own counter will mean that array2 wont directly rely on the index of array. When it checks 1 in array the index of array will be 0 and so would array2 but when it checks 15 the index of array would 4 but for array2 it would only be 1 since its index only gets incremented separately and only when the if statement is true. Since it only enters the if statement twice, numsNotDivis will never reach 2 and try to access the third element in array2 which would throw an outOfBounds exception.

if you look for generic solution you need to loop two times through the array first loop to determine count of elements you need to copy and second this to do the actual copy so your code look like this

int count=0; 
for(int i=0;i<array.length; i++) 
        if(array[i]%4!=0) count++; 
int[] array2 = new int[count]; 
int index2=0; 
for(int i=0;i<array.length;i++) 
         if(array[i]%4!=0) 
              { 
                  array2[index2]=array[i]; 
                  index2++; 
              }