开发者

'Use a static array'... what does that even mean?

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 instanc开发者_开发技巧e 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'.


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.

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<Vehicle> 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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜