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 11 of 11

Thread: A 2d array problem

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    21
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default A 2d array problem

    Given a 2d array
    and

    4 variables that creates another 2d array.
    (top, left) and (bottom,right)

    (Those are the coordinates to form another 2d array.)
    These variables can partially cover the given 2d array. So, negative and values greater than the width and height of the given 2d array is involved.



    I have to figure out the overlapping 2d array created by these two 2d arrays and display it.
    Can you give my some hints?


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: A 2d array problem

    Can you show us with the example, what exactly you want?

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: A 2d array problem

    How do you get a shape with only one variable. You need an x and y value to be able to define a shape with 4 points: top, bottom, left, right. If you have only an x value then you can only define locations on a line.

  4. #4
    Junior Member
    Join Date
    Jun 2011
    Posts
    21
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: A 2d array problem

    (top, left) is the coordinates for the top left corner of the 2d array.
    (bottom,right) is the coordinates for the bottom right corner of the 2d array.

    So, we have 4 variables.
    top = 1;

    left = 1;
    bottom = 2;

    right = 2;


    we'll have a 2x2 matrix.
    Last edited by IAmHere; January 24th, 2012 at 11:42 AM.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: A 2d array problem

    Ok, the 4 numbers define two points: x1,y1 and x2, y2
    and x1 < x2 and y1 < y2

    Where does the test for overlap come in?
    Are there two sets of the two points?

  6. #6
    Junior Member
    Join Date
    Jun 2011
    Posts
    21
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: A 2d array problem

    Sorry for the confusion.

    The first 2d array is:
    int[][] image1 = { {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}, };
    The second 2d array is:
    top = 1;
    bottom = 2;
    left = 1;
    right = 2;


    It means the second is entirely overlapping the first 2d array.

    5, 6
    8, 9

    is the overlapping value.

    but I can't figure out what if the second 2d array is partially overlapping the first array.

    eg. top = -4;
    bottom = 2;
    left = 1;
    right = 2;

    I have to find the overlapping values.
    There are a lot of cases that I have to cover.
    Is there a fast way of doing this?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: A 2d array problem

    How are the points x,y value placed in the array?
    Something like this: {{p1x, p1y}, {p2x,p2y}

    What are the point's x,y values in this array:
    int[][] image1 =
    { {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}, };

    How many points are there in that array? Can you label the x,y values for each point?
    pxi and pyi where i is the point number and pxi is the x value for point i

    When you say overlap I think in terms of shapes drawn on a 2 dimensional (x,y) surface.

  8. #8
    Junior Member
    Join Date
    Jun 2011
    Posts
    21
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: A 2d array problem

    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}

    1 is (0,0)
    2 is (1,0)
    3 is (2, 0)
    4 is (0, 1)
    .
    .
    .

    So,

    top = 1;
    bottom = 2;
    left = 1;
    right = 2;

    would be (top, left) and (bottom, right). These two coordinate are two corners of the second 2d array.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: A 2d array problem

    Is this what you mean:
    1 is at location (0,0)
    2 is at location (1,0)
    3 is at location (2, 0)
    4 is at location (0, 1)
    .

    The values in () are the indexes to the location in the array??
    {{(0,0) (1,0) (2, 0)}
    {(0,1) (1,1) (2,1)}
    etc

    So,

    top = 1;
    bottom = 2;
    left = 1;
    right = 2;

    would be (top, left) and (bottom, right). These two coordinate are two corners of the second 2d array.
    What does that mean?
    Last edited by Norm; January 24th, 2012 at 02:15 PM.

  10. #10
    Junior Member
    Join Date
    Jun 2011
    Posts
    21
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: A 2d array problem

    I meant
    4 is at location image1[0][1]
    4 is the value.


    so,

    top = 1;
    bottom = 2;
    left = 1;
    right = 2;

    Will give us the values:

    5, 6
    8, 9

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: A 2d array problem

    top = 1;
    bottom = 2;
    left = 1;
    right = 2;

    Will give us the values:

    5, 6
    8, 9
    When you say top or bottom do you mean the column (or row)?
    And then left or right is the row (or column)?
    Then the element at top=1,left=1 is is on the first row in the first column

    This would give ONE of the values (the x or the y?) of the four needed to define two points.
    How do you get the other value (the y or the x) needed for the point.
    How do you get the indexes for the other point(each point has an x and y value)

Similar Threads

  1. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  2. array problem
    By aizen92 in forum Java Theory & Questions
    Replies: 4
    Last Post: December 18th, 2010, 10:06 AM
  3. [SOLVED] array problem
    By darego in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 12th, 2010, 05:15 PM
  4. [SOLVED] Array Problem
    By KevinGreen in forum Object Oriented Programming
    Replies: 2
    Last Post: January 24th, 2010, 08:07 PM
  5. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM