Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing data in an array of objects???

Status
Not open for further replies.

Maldini

Technical User
Nov 10, 2002
55
GB
I have this class

public class Box
{
int width;
int height;
int depth;

public Box(int a, int b, int c)
{
width = a;
height = b;
depth = c;
}

public void getWidth()
{
System.out.println(width);
}

public void getHeight()
{
System.out.println(height);
}

public void getDepth()
{
System.out.println(depth);
}
}


and this collection of boxes...


public class Collecton
{
int count = 0;
Box[] BoxCollection;

public Collection
{
BoxCollection = new Box[50]
for (count = 0; count < 50; count++){
BoxCollection = new Box(0,0,0)
}
}

public recbox(int a, int b, int c)
{
BoxCollection[count] = new Box(a, b, c)
count++
}

public printBoxes()
{
System.out.println(&quot;Box Number = &quot; + count);
System.out.print(&quot;Box Width: &quot;);
BoxCollection[count].getWidth();
etc...
}
}


Now, I call this in the main code so that after each box is created, the specs of the box is printed back out onto the screen. The calling procedure works
However, only the Box Number part seems to print... the width, height and depth all don't work.

Am I accessing the data stored in the array of objects incorrectly?
Please advise.

Wierd thing is, if I add in a println line in my recbox method, like

System.out.println(&quot;W = &quot;);
BoxCollection.getWidth()

where is the number in the array, it prints out my info correctly. Otherwise by my print emthod, I am given the default 0,0,0 used to initialize the array

Thanks
 
Hi Maldini,
try
Code:
  System.out.println (Box Width: &quot; + boxCollection [count].getWidth ());
instead of:
Code:
  System.out.print(&quot;Box Width: &quot;);
  BoxCollection[count].getWidth();
Hope this Helps,
MarsChelios
 
thx for the reply MarsChelios

Just tried it out but the result is the same, the array still doesn't seem able to retrieve
 
Your missing a reference to the array index in yourconstructor:
Code:
public Collection(){
   BoxCollection = new Box[50]
   for (count = 0; count < 50; count++){
      BoxCollection[i] = new Box(0,0,0)
--------------------
^
Code:
   }
}

Either that or the forum thought it was supposed to be an italics TGML tag (if you didn't use code tags) but I don't see italics, so...


Hope that clears this up,
-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
One additonal note, your get functions should be written to return the values rather than just print them out, in case you would like to do something mathematical with them, why not write a toString function in your box like so:
Code:
public String toString(){
   String s = new String();
   s = &quot;Width: &quot; + width + &quot;\nHeight: &quot; + height + &quot;\nDepth: &quot; + depth;
   return s; 
}

Then rewrite your get functions to return numbers, like so:
Code:
public int getWidth(){
       return width;
}

There is nothing really wrong with the way you have it now, but this will not only add functionality to your box object, but it will also be easier to use later on, as the general format of any getParameter function is to return the parameter the method is named after, not print it out. toString() is also a common method in almost every object that is written or overrided to provided a string representation of the object. This way if your methods are written to act as other common objects do, you may forget what the functions do, but as long as you assume they work like the same methods in other objects you will be fine.

Also, your printBoxes function (the way it is written) will only print out the last box created. You should set it up to loop through the box collection like so:
Code:
public void printBoxes(){
   int i;
   for(i = 0;i < count+1;i++){
      System.out.println(&quot;--- Box &quot; + i + &quot;\n&quot; + BoxCollection[i].toString());
   }
}

public void printSingleBox(int boxNum){
   if(boxNum <= count){
      System.out.println(&quot;--- Box &quot; + i + &quot;\n&quot; + BoxCollection[i].toString());
   }
}

public void printBoxes(int start, int end){
   int i;
   if((start >= 0) && (end <= count)){
      for(i = start;i <= end;i++){
         System.out.println(&quot;--- Box &quot; + i + &quot;\n&quot; + BoxCollection[i].toString());
      }
   }
}


Also you have mis-spelled Collection in your : public class Collection, may want to fix that if you copied and pasted.

Just some additional thoughts and minor corrections,

-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top