• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Array of Objects

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?
 
Alireza Bahmanpour
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After testing it, I still don't get what I want!
All the elements of the array are still null

Any suggestions?
 
author
Posts: 23931
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your code should work fine. Could you show us what code you have writen before and after the 'for' loop ?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic