简体   繁体   English

多维数组:Java

[英]Multi-Dimensional Arrays: Java

I am working on a tax calculator and had the idea of putting the tax brackets into a multi-dimensional array. 我正在开发一个税收计算器,并且想到了将税收括号放入多维数组中。 This is the first time I have tried such a thing and was wondering if this is the correct way of doing it? 这是我第一次尝试这种方法,并且想知道这是否是正确的方法?

private static double[][][] taxBrackets;

static {
    taxBrackets = new double[20][14][14];
/*Single*/                           /*Married*/                        /*Head of Household*/
    //TierOne                           //TierOne                           //TierOne
    taxBrackets[0][0][0] = 0;           taxBrackets[0][1][0] = 0;           taxBrackets[0][0][1] = 0;       //MIN
    taxBrackets[1][0][0] = 9075;        taxBrackets[0][2][0] = 18150;       taxBrackets[0][0][2] = 12950;   //MAX
    taxBrackets[2][0][0] = 0.10;                                                                         //Tax Rate
    //TierTwo
    taxBrackets[3][0][0] = 9076;        taxBrackets[0][3][0] = 18151;       taxBrackets[0][0][3] = 12951;   //MIN
    taxBrackets[4][0][0] = 36900;       taxBrackets[0][4][0] = 73800;       taxBrackets[0][0][4] = 49400;   //MAX
    taxBrackets[5][0][0] = 0.15;                                                                         //Tax Rate
    //TierThree
    taxBrackets[6][0][0] = 36901;       taxBrackets[0][5][0] = 73801;       taxBrackets[0][0][5] = 49401;   //MIN
    taxBrackets[7][0][0] = 89350;       taxBrackets[0][6][0] = 148850;      taxBrackets[0][0][6] = 127550;  //MAX
    taxBrackets[8][0][0] = 0.25;                                                                         //Tax Rate
    //TierFour
    taxBrackets[9][0][0] = 89351;       taxBrackets[0][7][0] = 148851;      taxBrackets[0][0][7] = 127551;  //MIN
    taxBrackets[10][0][0] = 186350;     taxBrackets[0][8][0] = 226850;      taxBrackets[0][0][8] = 206600;  //MAX
    taxBrackets[11][0][0] = 0.28;                                                                        //Tax Rate
    //TierFive
    taxBrackets[12][0][0] = 186351;     taxBrackets[0][9][0] = 226851;      taxBrackets[0][0][9] = 206601;  //MIN
    taxBrackets[13][0][0] = 405100;     taxBrackets[0][10][0] = 405100;     taxBrackets[0][0][10] = 405100; //MAX
    taxBrackets[14][0][0] = 0.33;                                                                        //Tax Rate
    //TierSix
    taxBrackets[15][0][0] = 405101;     taxBrackets[0][11][0] = 405101;     taxBrackets[0][0][11] = 405101; //MIN
    taxBrackets[16][0][0] = 406750;     taxBrackets[0][12][0] = 457600;     taxBrackets[0][0][12] = 432200; //MAX
    taxBrackets[17][0][0] = 0.35;                                                                        //Tax Rate
    //TierSeven
    taxBrackets[18][0][0] = 406751;      taxBrackets[0][13][0] = 457601;     taxBrackets[0][0][13] = 432201; //MIN
    taxBrackets[19][0][0] = 0.396;                                                                       //Tax Rate
}

The idea was to have Single filers in the left array, Married/Joint filers in the middle and Head of Household filers in the right array. 这个想法是在左边的阵列中有“单身”文件管理器,在中间的是“已婚/联合”文件管理器,而在右边的阵列中是“户主”文件管理器。 Did I do this correctly or did I miss the point entirely? 我是否正确地做到了这一点?还是我完全错过了要点?

UPDATE: 1/30/2017 After following Nhouser9's suggestions I ended up with this which is much more manageable. 更新:1/30/2017在遵循了Nhouser9的建议之后,我最终得出了这个结论,它更易于管理。

private static class TaxBracket {

    final double minSalary;
    final double maxSalary;
    final double taxRate;

    TaxBracket(double minSalary, double maxSalary, double taxRate) {
        this.minSalary = minSalary;
        this.maxSalary = maxSalary;
        this.taxRate = taxRate;
    }
}
//This is the data structure which holds the values for each tier of each filing status
//Changing values in these arrays will affect the output of the entire program
private static TaxBracket[] singleFiler;
static {
    singleFiler = new TaxBracket[]
        {
            new TaxBracket(0, 9075, 0.10),      //Index 0 TierOne
            new TaxBracket(9076, 36900, 0.15),  //Index 1 TierTwo
            new TaxBracket(36901, 89350, 0.25), //Index 2 TierThree
            new TaxBracket(89351, 186350, 0.28),//Index 3 TierFour
            new TaxBracket(186351, 405100, 0.33),//Index 4 TierFive
            new TaxBracket(405101, 406750, 0.35),//Index 5 TierSix
            new TaxBracket(406751, Double.MAX_VALUE, 0.396)//Index 6 TierSeven
        };
}
private static TaxBracket[] jointFiler;
static {
    jointFiler = new TaxBracket[]
        {
            new TaxBracket(0, 18150, 0.10),      //Index 0 TierOne
            new TaxBracket(18151, 73800, 0.15),  //Index 1 TierTow
            new TaxBracket(73801, 148850, 0.25), //Index 2 TierThree
            new TaxBracket(148851, 226850, 0.28),//Index 3 TierFour
            new TaxBracket(226851, 405100, 0.33),//Index 4 TierFive
            new TaxBracket(405101, 457600, 0.35),//Index 5 TierSix
            new TaxBracket(457601, Double.MAX_VALUE, 0.396)//Index 6 TierSeven
        };
}
private static TaxBracket[] hohFiler;
static {
    hohFiler = new TaxBracket[]
        {
            new TaxBracket(0, 12950, 0.10),      //Index 0 TierOne
            new TaxBracket(12951, 49400, 0.15),  //Index 1 TierTow
            new TaxBracket(49401, 127550, 0.25), //Index 2 TierThree
            new TaxBracket(127551, 206600, 0.28),//Index 3 TierFour
            new TaxBracket(206601, 405100, 0.33),//Index 4 TierFive
            new TaxBracket(405101, 432200, 0.35),//Index 5 TierSix
            new TaxBracket(432201, Double.MAX_VALUE, 0.396)//Index 6 TierSeven
        };
}

This is one possible solution. 这是一种可能的解决方案。 If it works, it's fine. 如果有效,那很好。

Since Java is an Object Oriented language, It would be better to use Objects. 由于Java是一种面向对象的语言,因此使用对象会更好。 For example, TaxBracket object like this: 例如, TaxBracket对象是这样的:

public class TaxBracket {
    public static final int SINGLE = 1;
    public static final int MARRIED = 2;
    public static final int HEAD_OF_HOUSE = 3;

    public final int mStatus;
    public final int minSalary;
    public final int maxSalary;
    public final double rate;

    public TaxBracket(int mStatus, int minSalary, int maxSalary, double rate) {
        this.mStatus = mStatus;
        this.minSalary = minSalary;
        this.maxSalary = maxSalary;
        this.taxRate = taxRate;
    }
}

Then instead of multi-dimensional arrays you could declare a single array of these objects, initializing it like this: 然后,您可以声明这些对象的单个数组,而不是多维数组,并按如下所示对其进行初始化:

taxBrackets[0] = new TaxBracket(TaxBracket.SINGLE, 0, 9075, .1);

No. It is not the best way to do that. 不。这不是最好的方法。 I would implement three classes for Single , Married and HeadOfHousehold . 我将为SingleMarriedHeadOfHousehold实现三个类。

Each of this class would implement an interface, eg Married implements TaxTiers . 每个此类都将实现一个接口,例如Married implements TaxTiers

interface TaxTiers() {

  public int getMaxForTier(int tier) {
  }

  public int getMinForTier(int tier) {
  }

  public double getRateForTier(int tier) {
  }
}

In classes Single , Married and HeadOfHousehold you can have 3 arrays or List s: tierMins , tierMaxs and tierRates . SingleMarriedHeadOfHousehold类中,您可以具有3个数组或ListtierMinstierMaxstierRates Then you just write: 然后,您只需编写:

public double getRateForTier(int tier) {
  return tierRates.get(tier);
}

I would prefer to create a class for the tax bracket contains the attributes Single , Married , HeadOfHousehold and all other attributes you have. 我更愿意为税级创建一个类,其中包含属性SingleMarriedHeadOfHousehold以及您拥有的所有其他属性。 And, initiate a single dimension array of instances of that class. 并且,启动该类实例的一维数组。 Like the following: 如下所示:

class TaxBracket{
    double single;
    double married;
    double headOfHousehold;

    .... // any other attributes + getters and setters! 

}

In the main class, you define the array: 在主类中,定义数组:

TaxBracket []taxBrackets = new TaxBracket[20];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM