• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

having problem with a 2 dimensional array

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have this 2 dimensional array that represents color pixels
RGBColor[][] rgbArray1 = new RGBColor[rows][columns] - so each elemnt in this array is a pixel - (red, green, blue)

i wrote a class named RGBImage. represents an image. the constructor of this class takes RGBColor[][] rgbArray1 as an argument
and print the image.

my problem is when i try to rotate the image 90 degrees. because the dimentions are changeing. i'm getting ArrayIndexOutOfBounce Exception.
i know i can to this by create a bigger 2 dimensional array and copy the original , but for this assignment i cant do this .
i must use the original one . now i will show the main method that will get things clearer if i didnt explained well enought.



output:


this outputs are what i need to accomplish.
 
Saloon Keeper
Posts: 11127
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We need to see more code. What is 'rotateClockwise()' ?
 
Carey Brown
Saloon Keeper
Posts: 11127
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your original is 4x3 then your rotated image must be 3x4. This is not bigger just different dimensions.
 
Ranch Hand
Posts: 64
Netbeans IDE Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it a requirement that the RGBColor array of arrays held by RGBImage has to change shape, or can RGBImage know it is rotated 90 degrees and print accordingly?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dan D'amico wrote:this outputs are what i need to accomplish.


OK, but the "what" is actually more generic than that, and shouldn't matter whether your array contains RGB values, letters, or multi-coloured bouncing balls.

What you need to do is think:
1. (as Carey already pointed out) in your specific case, a 4 x 3 array will be a 3 x 4 array when it's "rotated". But what if it was a 6 x 3 array? or 137 x 43?
2. Where does each cell from the original array go into a "rotated" one? For example, where does (0,0) end up? And what about (0,1)? Or (1,0)? Go through a few of these and see if a pattern emerges - especially one that relates to the width and height of the original, because then you'll be able to implement it for ANY 2D array.
Just as a kick-start, (0,0) will end up at (0,2) in your "rotated" array, but the important question is why ... and how does it relate to the original width and height?

I'd suggest using copious amounts of paper and a pencil, and drawing lots of diagrams - and don't write one line of Java code until you really understand what it's going to do.

HIH

Winston
 
Dan D'amico
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Dan D'amico wrote:this outputs are what i need to accomplish.


OK, but the "what" is actually more generic than that, and shouldn't matter whether your array contains RGB values, letters, or multi-coloured bouncing balls.

What you need to do is think:
1. (as Carey already pointed out) in your specific case, a 4 x 3 array will be a 3 x 4 array when it's "rotated". But what if it was a 6 x 3 array? or 137 x 43?
2. Where does each cell from the original array go into a "rotated" one? For example, where does (0,0) end up? And what about (0,1)? Or (1,0)? Go through a few of these and see if a pattern emerges - especially one that relates to the width and height of the original, because then you'll be able to implement it for ANY 2D array.
Just as a kick-start, (0,0) will end up at (0,2) in your "rotated" array, but the important question is why ... and how does it relate to the original width and height?

I'd suggest using copious amounts of paper and a pencil, and drawing lots of diagrams - and don't write one line of Java code until you really understand what it's going to do.

HIH

Winston



ok. before i wrote this thread. i actually did what most of you said and yes. i wrote the pattern on a paper .
00 is 02
01 is 12
etc...
my original array is 3,4
so i managed to create in the class another array of 4,3
and copied the elemends of the 3,4 array via a for loop that does what i wrote on the paper.
and when i print this new 4 on 3 array it printed just fine as expected.
the problem that i need to print the original image. through the main method as shown above.
so after i had this new 4,3 array. i just did.
originalArray(3,4) = newArray(4,3)

but then i get an Exception error. ArrayIndexOutOfBounds
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dan D'amico wrote:but then i get an Exception error. ArrayIndexOutOfBounds


Because Java array indexes start from 0.

Think about it: if they didn't, you couldn't possibly have a (0, 0) object could you?

So: given that you now know that, what is the largest index you can have for a [4][3] array?

Winston
 
Dan D'amico
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Dan D'amico wrote:but then i get an Exception error. ArrayIndexOutOfBounds


Because Java array indexes start from 0.

Think about it: if they didn't, you couldn't possibly have a (0, 0) object could you?

So: given that you now know that, what is the largest index you can have for a [4][3] array?

Winston



12.2
but it still not worked. give me the exception error.
maybe i need to check my constructor.
my attributes are the height the width and a 2 dim array.
maybe its just my constructor and how i used the attributes.
because the logic of the for loop is good.
even if i assigns the new 4,3 to the original 3,4 through a for loop and the body of the loop is
originalArray[i][j] = newArray[i][j]
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dan D'amico wrote:12.2
but it still not worked. give me the exception error.


Because your answer is wrong. One part of it is right, but the other isn't (where do you get '12' from?)

Winston
 
Dan D'amico
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Dan D'amico wrote:12.2
but it still not worked. give me the exception error.


Because your answer is wrong. One part of it is right, but the other isn't (where do you get '12' from?)

Winston



i declare the 2 dim array and when initilaize it to 3,4
it can contains no more than 12 elements
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dan D'amico wrote:i declare the 2 dim array and when initilaize it to 3,4
it can contains no more than 12 elements


So, even if your assumption was correct (which it isn't), the largest index value would be 11, wouldn't it?

But you have a 2D array, so you have two indexes: one for the "row", one for the "column".

Think about it a bit more, and see if you can't work it out for yourself (you'll be much prouder if you do). And if you really can't fathom it, come back.

Winston
 
Dan D'amico
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Dan D'amico wrote:i declare the 2 dim array and when initilaize it to 3,4
it can contains no more than 12 elements


So, even if your assumption was correct (which it isn't), the largest index value would be 11, wouldn't it?

But you have a 2D array, so you have two indexes: one for the "row", one for the "column".

Think about it a bit more, and see if you can't work it out for yourself (you'll be much prouder if you do). And if you really can't fathom it, come back.

Winston


yes i know because its starts from 0.
ok. i will be back with sucssess or with question . thanks
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like an ITIS 1213 assignment to me. Yes?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic