How to declare a static array of user-defined data types with hard-coded values?

Purpose:

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

Plan:

My plan was to define a custom data type, for example ...

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 set the array this way ...

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

However, this is not being built. I continue to see the message " ". Type mismatch: cannot convert from float[] to ScalingData

How can I achieve my goal?

UPDATE

I tried to implement the proposals so far, but still ran into an error ...

The code looks like this:

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 an error with a hard-coded array

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 in accordance with the 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;
        }
    }
}
+3
5

Java. :

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

ScalingData, .

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

: float double, . , .

+1

, , , new ScalingData(1.01f, "Data point 1", 1) {1.01f, "Data point 1", 1}. .

ScalingData static, , , , , CustomConverter ( new , : myCustomConverterInstance.new ScalingData(1.01f, "Data point 1", 1) - ScalingData , ).

, , , . , . , ScalingData, , , , . , , , :

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),
    ...
};

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

Java, - scalingDataArray scalingDataArray ( ).

+1
source

You cannot expect some random data to magically transform into an object.

You will need to instantiate the ScalingData objects.

0
source

I think an enumeration 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;
    }
}
0
source

All Articles