File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Problems with arrays & for loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problems with arrays & for loops" Watch "Problems with arrays & for loops" New topic
Author

Problems with arrays & for loops

Karl Davies
Greenhorn

Joined: Sep 21, 2010
Posts: 6
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.

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3415
    
  12
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


Joanne
Karl Davies
Greenhorn

Joined: Sep 21, 2010
Posts: 6
Brilliant! It's in working order now, thank you Joanne!
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Why not create a 2D array?


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
And welcome to the Ranch
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Problems with arrays & for loops
 
Similar Threads
loop over array to check for duplicates
Exception in thread "main" java.lang.NoClassDefFoundError: kolos/Main
diamond pattern without parse.int (Someone help me T.T) :cry:
Please help in this selection sort
Java histogram program.. bit of help required please?