Question

Goal:

I want to implement a hard-coded lookup table for data that doesn't change often, but when it does change I want to be able to quickly update the program and rebuild.

Plan:

My plan was to define a custom data type like so...

private class ScalingData
{
    public float mAmount;
    public String mPurpose;
    public int mPriority;

    ScalingData(float fAmount, String strPurpose, int iPriority)
    {
        mAmount = fAmount;
        mPurpose = strPurpose;
        mPriority = iPriority;
    }
}

and then, in the main class, hard-code the array like so ...

public static ScalingData[] ScalingDataArray =
{
        {1.01f, "Data point 1", 1},
        {1.55f, "Data point 2", 2}
};

However, this doesn't build. I keep seeing the message "Type mismatch: cannot convert from float[] to ScalingData".

How can I achieve my objective ?

UPDATE

I've attempted to implement the suggestions so far, but still running into an error...

The code looks like:

public class CustomConverter
{
    //Lookup Table
    private static ScalingData[] ScalingDataArray =
    {
        new ScalingData(1.01f, "Data point 1", 1),
        new ScalingData(1.55f, "Data point 2", 2)
    };


    //Constructor
    CustomConverter()
    {
        //does stuff
    }


    //Custom Data type
    private class ScalingData
    {
        public float mAmount;
        public String mPurpose;
        public int mPriority;

        ScalingData(float fAmount, String strPurpose, int iPriority)
        {
            mAmount = fAmount;
            mPurpose = strPurpose;
            mPriority = iPriority;
        }
    }
}

and the error with the hard-coded array is

No enclosing instance of type CustomConverter is accessible.
   Must qualify the allocation with an enclosing instance of type CustomConverter
   (e.g. x.new A() where x is an instance of CustomConverter).

EDIT... complete solution as per answers below

public class CustomConverter
{
    //Lookup Table
    private static ScalingData[] ScalingDataArray =
    {
        new ScalingData(1.01f, "Data point 1", 1),
        new ScalingData(1.55f, "Data point 2", 2)
    };


    //Constructor
    CustomConverter()
    {
        //does stuff
    }


    //Custom Data type
    private static class ScalingData
    {
        public float mAmount;
        public String mPurpose;
        public int mPriority;

        ScalingData(float fAmount, String strPurpose, int iPriority)
        {
            mAmount = fAmount;
            mPurpose = strPurpose;
            mPriority = iPriority;
        }
    }
}
Was it helpful?

Solution

You can't do that in Java. You need to use the constructor like so:

public static ScalingData[] ScalingDataArray =
{
        new ScalingData(1.01f, "Data point 1", 1),
        new ScalingData(1.55f, "Data point 2", 2)
};

OTHER TIPS

You array contains ScalingData so you have to add instance of those.

public static ScalingData[] ScalingDataArray = {
        new ScalingData(1.01f, "Data point 1", 1),
        new ScalingData(1.55f, "Data point 2", 2)
};

BTW: I wouldn't use float instead of double unless you really need to. Most of the time having the extra precision is more useful than the few bytes you save.

As others have already written, you need to explicitly create instances of your object, that is your array needs items such as new ScalingData(1.01f, "Data point 1", 1) instead of {1.01f, "Data point 1", 1}. It's a matter of the language's syntax.

For your extended question, the nested class ScalingData should be declared static, otherwise, just as the error message says, it would need to be created from within an enclosing instance of the class it's nested in, CustomConverter (or you would have to call operator new using an instance of this type, like so: myCustomConverterInstance.new ScalingData(1.01f, "Data point 1", 1) - but ScalingData doesn't need the enclosing class, just declare it static or declare it as a non-nested class).

If your array has many points and the you find the class's name a bit too long, as a shorthand you can create a helper function with a very short name which just creates an instance. Slightly long names are good for readability, but long array's initialization may be an exception sometimes. In this particular case where the name is just ScalingData, the gain isn't big but with some class names I do find this solution to make code better, especially if the array is long. It's just a hack but it allows the array initialization code to be shorter and possibly more legible:

protected static ScalingData point(float fAmount, String strPurpose, int iPriority) {
    return new ScalingData(fAmount,strPurpose,iPriority);
}

public static ScalingData[] ScalingDataArray = {
    point(1.01f, "Data point 1", 1),
    point(1.55f, "Data point 2", 2),
    ...
};

instead of

public static ScalingData[] ScalingDataArray = {
    new ScalingData(1.01f, "Data point 1", 1),
    new ScalingData(1.55f, "Data point 2", 2),
    ...
};

On a side note, it's usually good to follow Java naming conventions, meaning your array's name should start with a lowercase letter - scalingDataArray instead of ScalingDataArray (uppercase first is for classes).

You can't expect some random data to magically transform into an Object.

You would need to instantiate your ScalingData objects.

I think an enum might better suit your needs:

enum ScalingData{

    DataPoint1(1.01f, "Data Point 1", 1),
    DataPoint2(1.55f, "Data Point 2", 2);

    final float Amount;
    final String Purpose;
    final int Priority;

    ScalingData(float fAmount, String strPurpose, int iPriority){
        Amount = fAmount;
        Purpose = strPurpose;
        Priority = iPriority;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top