Forums Register Login
Problems with arrays & for loops
Hello! I'm incredibly new to Java and programming. The problem I'm having with this piece of code is the marked for loop (or at least I think it is), I want it to take all all elements below 10 and copy them into array10 but it just isn't doing that and I'm not sure where I've gone wrong. Help would be brilliant Thanks.

In your MARKED for loop, x is not the value of an array element - it's the index of an array element. The value is represented by randArray[x].

Also, you don't need to use system.arraycopy to copy a single array element. Just use

And just for the record, your parameters to System.arraycopy are wrong. The last parameter is the number of values to copy, so the first time round the loop you copied none, the second time one, the third time two, etc
Brilliant! It's in working order now, thank you Joanne!
Why not create a 2D array?
And welcome to the Ranch
He baked a muffin that stole my car! And this tiny ad:
Become a Java guru with IntelliJ IDEA

This thread has been viewed 915 times.

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2019 13:22:40.