Results 1 to 11 of 11
  1. #1
    xXRedneckXx is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default Trouble copying an array

    Okay fine I got it
    Last edited by xXRedneckXx; 02-05-2011 at 05:07 PM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    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++)
    what will happen when this line tries to execute? And
    Java Code:
    for(int print=0; print<numbers.length ;print++)
    when the length of numbers is reduced inside the loop, what will happen to the loop current executing?

  3. #3
    xXRedneckXx is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    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...

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by xXRedneckXx View Post
    I apologize for no code tags.
    Don't apologize -- fix the problem by editing your first post and adding the tags. Please see my first signature link to see what needs to be done.

  5. #5
    xXRedneckXx is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    Code:
    for(int index3=numbers.length/2; index3<0; index3++)
    what will happen when this line tries to execute?
    I can see how that doesnt really work but if I use index3-- which is what I actually had originally i dont get a different output
    Last edited by xXRedneckXx; 02-05-2011 at 03:41 AM.

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Quote Originally Posted by xXRedneckXx View Post
    I can see how that doesnt really work but if I use index3-- which is what I actually had originally i dont get a different output
    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)

  7. #7
    xXRedneckXx is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    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			  			  			  			  
    			  }
    			  }
    So now its not returning 0's I'm getting the last number in the text file repeatedly..

  8. #8
    xXRedneckXx is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    Im still kinda lost on this problem soo if anybody has any suggestions id like to hear it.

  9. #9
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

  10. #10
    xXRedneckXx is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    Wasnt aware it was the same website...Since I had to register again...

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by xXRedneckXx View Post
    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 05:41 PM.

Similar Threads

  1. Having trouble with 2d Array
    By geekchick in forum New To Java
    Replies: 2
    Last Post: 08-02-2010, 04:54 AM
  2. trouble with array method
    By kpro862 in forum New To Java
    Replies: 6
    Last Post: 05-12-2010, 12:08 AM
  3. Array trouble
    By hobo in forum New To Java
    Replies: 8
    Last Post: 11-17-2009, 12:04 PM
  4. Copying ArrayList into an Array
    By Manfizy in forum New To Java
    Replies: 6
    Last Post: 07-16-2009, 07:03 AM
  5. Having trouble with array
    By ice22 in forum New To Java
    Replies: 3
    Last Post: 11-13-2007, 03:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •