Get your CodeRanch badge!*
The moose likes Java in General and the fly likes Array of Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Array of Objects" Watch "Array of Objects" New topic
Author

Array of Objects

Alireza Bahmanpour
Greenhorn

Joined: Oct 24, 2007
Posts: 23
Hi

I have a class Pixel(x, y, filled) which holds the properties of a single pixel in a BufferedImage. x and y are simply the coordinates, and 'filled' is a boolean that specifies whether the pixel is filled or not (its really long to explain the whole thing, so this is just to give you an idea of what's going on)

Now I want to create a 2d array the size of my BufferedImage (Pixel[][] pixelArray = new Pixel[width][height]), holding a new Pixel object in each cell with its own properties. Is it possible to use the following for-loop for this matter? Or do I have to change the name of the Pixel object every time?




Thanks in advance
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Is it possible to use the following for-loop for this matter?

Yes. It looks fine.

Or do I have to change the name of the Pixel object every time?

The name ? Which name ? Why would you give it a name ?


[My Blog]
All roads lead to JavaRanch
Alireza Bahmanpour
Greenhorn

Joined: Oct 24, 2007
Posts: 23
After testing it, I still don't get what I want!
All the elements of the array are still null

Any suggestions?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18136
    
  39

Alireza Bahmanpour wrote:After testing it, I still don't get what I want!
All the elements of the array are still null


The only way for it to be all null is if you didn't set it. And the only way that I can see that happen is if width and height isn't set properly.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Evan Caballero
Ranch Hand

Joined: Dec 10, 2009
Posts: 59
your code should work fine. Could you show us what code you have writen before and after the 'for' loop ?
 
Consider Paul's rocket mass heater.
 
subject: Array of Objects
 
Similar Threads
Number of colors in an Image
create image from rgb values
Converting a two dimensional int array into a byte array
Rotating a buffered image
Picture Manipulation Methods