tags:

views:

90

answers:

2

I know what a static array is and how to use it in Java, but my Prof assigned us a program and for one of the many classes we had to create, he asked us to 'use a static array' in such a way that multiple objects will store their data there.

For instance, if the objects were car garages, then each garage instance would store in a 100x3 static array their data:

 1, honda, four-door
 3, toyota, two-door
 1, bicycle, -1
 1, ford, pickup
 2, ford, fiesta
 3, chevy, two-door
 3, bicycle, -1

The -1 indicates the end of each garage.

That's a lousy example, but you get the idea.

So, I am thinking that what he wants is kind of like this:

In the demo class (which is the main), I will declare an instance of a class I made with the static array in it:

      PublicAccessArray p1 = new  PublicAccessArray();

Then that class starts off like:

      public class PublicAccessArray {

      public static int[][] accessArray;

      public static void PublicAccessArray()
      {
          accessArray = new int[100][3];
      ...

And then my class that will create the objects to use the static array will look like:

      public class ClassThatUsesTheStaticArray {
           public void ClassThatUsesTheStaticArray (PublicAccessArray array1)
           {...

So then back in the demo/main class I would instantiate those objects with:

      ClassThatUsesTheStaticArray  c1= new ClassThatUsesTheStaticArray (p1);

I think that's what he means, and I know that the static array should look like the example I gave (though it uses numbers as data/elements).

I know he wants us to instantiate the ClassThatUsesTheStaticArray objects from the demo/main class and the only way I can see to do that would be to pass the p1 array to it.

Does what I am doing seem to be the right way? I often an easier time doing the actual coding than figure out what my Prof. is actually asking us to do so I was wondering if that sounds like a way to 'use a static array'.

+1  A: 

In your example the static array is defined as int[100][3], which is an int array. This would not fit in your example. You can declare it as Object array, but that's ugly.

On a different note, please keep in mind other than academic exercise, static fields have to be use sparingly.

kuriouscoder
My fault, the example was just for illustration. The actual array IS an int array. Yes, the use of the static array is just my prof's exahustive way of making us use all of the possibilities.
Quinn1000
+1  A: 

I do not think that you are on a right way. First you cannot store "honda" or "toyota" in int array. So you need String array. The first column looks like index, so you do not have to store it in your array at all.

Second, the example is not OO enough. I'd suggest you the following.

  1. create class vehicle:

public abstract class Vehicle { private String name; // getters, setters, constructor... }

Then create classes Car and Bicycle that extend Vehicle. These classes can contain other data: public class Car extends Vehicle { public static enum Type {PICKUP, }; // add here other types private String model; private int doorsCount; private Type type; }

Now create class Garage: public class Garage { private List vehicles; // other fields and methods relevant for garage. }

This is the time to create static array of garages:

private static Garage[] garages = new Garage[100];

You can populate it using static initializer (I assume here existence of convenient constructors I did not mention in my explanation): static { Vehicle honda = new Car("honda", 4); Vehicle toyota = new Car("toyota", 2);
Vehicle bicycle = new Bicycle("bicycle");
garaghes[0] = new Garage("First", new Vehicle[] {honda, toyota, bicycle});

Vehicle fiesta = new Car("ford", "fiesta");
Vehicle pickup = new Car("ford", Car.Type.PICKUP);
garaghes[1] = new Garage("Second", new Vehicle[] {fiesta, pickup});

}

I believe your Prof will be satisfied. Good luck!

AlexR
OK, forget my using the cars as an example. I'm not trying to put objects in an int array, just putting ints into an int array. I thought it might be more clear than saying the array would be: 1,2,3 4,5,6 1,2,-1 :-) I like your way of doing it, but when I look at the way he gave us for a demo example, it's going to have to be a matter of passing the object with the static array in it.
Quinn1000