Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Array overwriting

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Array overwriting

    a


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Array overwriting

    it overwrites the array even if there is a car already in that specific location.
    Can you use an if test to detect if the location is already occupied before changing it?

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array overwriting

    That's the problem, I don't know how to implement that ..
    Thanks for your quick reply

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Array overwriting

    How do you know there is a car in the location? What is in the array?
    The Arrays class's deepToString() method is useful for printing out the contents of a 2 dim array for debugging:
    System.out.println("an ID "+ java.util.Arrays.deepToString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array overwriting

    03,03,00,00,00,04
    02,00,00,08,00,04
    02,01,01,08,00,04
    02,00,00,08,00,00
    05,00,00,00,07,07
    05,00,06,06,06,00

    --->

    03,03,00,00,00,04
    02,00,00,08,00,04
    02,00,01,01,00,04
    02,00,00,08,00,00
    05,00,00,00,07,07
    05,00,06,06,06,00

    This is my gameboard, When I move Car 01 to the right, it overwrites a part of 08.
    In the code you can find how I move the cars. Now i'm looking for a solution which makes overwriting impossible

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Array overwriting

    The numbers in the array have no meaning for me.

    Can you answer this question?
    What value(s) in the array means that it is OK to move a car there?

    Use a if statement to detect if the array at the location has that value. If the array does not have that value, do not move the car there.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array overwriting

    The zeroes are empty spaces, the number are all different cars.
    So it's ok to move a car to a zero. As you can see above i made some methodes which move the car.
    So now I woud like a if clause which checks if the location which im moving to is 0. If it's 0, it can move, otherwise it shouldn't.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Array overwriting

    Are you asking how to write an if statement?
    See the tutorial: The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

    --- Update ---

    Please make the thread as solved.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: January 14th, 2013, 03:22 PM
  2. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  3. Overwriting txt file
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 29th, 2010, 08:21 AM
  4. 2d (4x4) array insdie a 1d array. (Block cipher)
    By fortune2k in forum Collections and Generics
    Replies: 13
    Last Post: November 23rd, 2010, 05:29 PM
  5. Problem with overwriting a file using IO package
    By marksquall in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: March 28th, 2009, 06:37 AM