Results 1 to 11 of 11
Thread: Trouble copying an array
- 02-05-2011, 02:10 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
- 02-05-2011, 03:47 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,638
- Rep Power
- 13
The code tags makes your code much more readable...that being said, think like a computer...step through the code one thing at a time and consider what happens. For example:
Java Code:for(int index3=numbers.length/2; index3<0; index3++)
Java Code:for(int print=0; print<numbers.length ;print++)
- 02-05-2011, 04:00 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
I apologize for no code tags. Well the first loop i believe will increment from the mid-point of the array so it would assign temp2[0]=numbers[4] for example..
As for the second part I thought that might be a problem as well and made a variable to just hold the value before the loop and use that instead. But when i do that i get an error reading
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at datastructures1.Main.main(Main.java:62)"
and it highlights the printing of the numbers...
-
- 02-05-2011, 04:22 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
Code:
for(int index3=numbers.length/2; index3<0; index3++)
what will happen when this line tries to execute?Last edited by xXRedneckXx; 02-05-2011 at 04:41 AM.
- 02-05-2011, 04:44 AM #6
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,638
- Rep Power
- 13
I don't think you see...think about that line. Set index3 equal to some positive value. Check if that value is less than zero...if so, continue, if not exit loop.
The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
- 02-05-2011, 05:15 AM #7
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
Oh okay I see this is what i have:
Java Code:for(int index2=0; index2<(numbers.length/2); index2++) { for(int index3=((numbers.length/2)-1); index3<numbers.length; index3++) { temp2[index2]=numbers[index3];//Inputs numbers from original to temporary half sized array } }
- 02-05-2011, 03:41 PM #8
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
Im still kinda lost on this problem soo if anybody has any suggestions id like to hear it.
- 02-05-2011, 05:54 PM #9
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,638
- Rep Power
- 13
- 02-05-2011, 05:57 PM #10
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
Wasnt aware it was the same website...Since I had to register again...
-
It's not the same website. Most volunteers feel that their own time is valuable, and mind it if a poster doesn't agree, and cross-posts the same question on multiple fora, thus increasing the chances of someone here or there wasting their time answering a question that has already been answered elsewhere. If you feel you must cross-post, be considerate and notify all threads of this. One of the posters in your cross-posted also gave you a link that goes into more detail about cross-posting and why you should notify all of this.
Also, why did you erase your initial post?Last edited by Fubarable; 02-05-2011 at 06:41 PM.
Similar Threads
-
Having trouble with 2d Array
By geekchick in forum New To JavaReplies: 2Last Post: 08-02-2010, 05:54 AM -
trouble with array method
By kpro862 in forum New To JavaReplies: 6Last Post: 05-12-2010, 01:08 AM -
Array trouble
By hobo in forum New To JavaReplies: 8Last Post: 11-17-2009, 01:04 PM -
Copying ArrayList into an Array
By Manfizy in forum New To JavaReplies: 6Last Post: 07-16-2009, 08:03 AM -
Having trouble with array
By ice22 in forum New To JavaReplies: 3Last Post: 11-13-2007, 04:06 AM
Bookmarks