字符串解析并从文本字符串获取数组索引

时间:2018-12-26 14:04:25

标签: java string hash

我解析了一个嵌套的Json文件,该文件具有嵌套的对象和数组,并在Map(String,Object)中得到结果。我能够得到的结果:

RoomStays.RoomStay.RoomRates.RoomRate [15] .Rates.Rate [0] .Base.DecimalPlaces RoomStays.RoomStay.RoomRates.RoomRate [15] .Rates.Rate [0] .Base.AmountBetaxTax RoomStays.RoomStay.RoomRates.RoomRate [15] .Rates.Rate [0] .EffectiveDate RoomStays.RoomStay.RoomRates.RoomRate [15] .Rates.Rate [0] .RateTimeUnit RoomStays.RoomStay.RoomRates.RoomRate [15] .Rates.Rate [1] .RateMode RoomStays.RoomStay.RoomRates.RoomRate [15] .Rates.Rate [1] .UnitMultiplier RoomStays.RoomStay.RoomRates.RoomRate [15] .Rates.Rate [1] .Base.CurrencyCode RoomStays.RoomStay.RoomRates.RoomRate [15] .Rates.Rate [1] .Base.DecimalPlaces RoomStays.RoomStay.RoomRates.RoomRate [15] .Rates.Rate [1]。税前基本金额 RoomStays.RoomStay.RoomRates.RoomRate [15] .Rates.Rate [1] .EffectiveDate RoomStays.RoomStay.RoomRates.RoomRate [15] .Rates.Rate [1] .RateTimeUnit RoomStays.RoomStay.RoomRates.RoomRate [15] .AvailabilityStatus RoomStays.RoomStay.RoomRates.RoomRate [15] .NumberOfUnits RoomStays.RoomStay.RoomRates.RoomRate [15] .Total.CurrencyCode

我现在需要知道索引号,例如15是RoomStays.RoomStay.RoomRates.RoomRate的索引。感谢帮助。预先感谢。

1 个答案:

答案 0 :(得分:0)

这可以在您的RoomRate数组没有重复率的情况下工作。如果将getArrayIndex数组的RoomRate函数与要为其建立索引的RoomRate实例一起传递,它将返回索引。

    public static void main(String[] args) {

        int[] RoomRate = new int[3];    
        RoomRate[0] = 5;
        RoomRate[1] = 10;
        RoomRate[2] = 20;

        int index = getArrayIndex(RoomRate, RoomRate[1]);

        System.out.println(index);
    }

    private static int getArrayIndex(int[] RoomRate, int instance) {        
        int index = 0;      
        for (int i = 0; i < RoomRate.length; i++) {         
            if (RoomRate[i] == instance) {
                index = i;
                break;
            }           
        }
        return index;
    }