简体   繁体   English

在Java中有效遍历具有多个变量类型的对象数组

[英]Efficiently Looping through Object array with Multiple Variable types in Java

I'm writing a simple script in Java that is calling another class that holds all my information. 我正在用Java写一个简单的脚本,该脚本正在调用另一个包含我所有信息的类。

I am holding my information in the called class in Object[] Arrays and I am planning on calling the script to fetch that array. 我将信息保存在Object [] Arrays的调用类中,并且打算调用脚本来获取该数组。

Right now the function looks like this. 现在,函数看起来像这样。

public void tradeShop() {

    /*
     *Variables must be initialized in order to call shopTrader
     *The values are just non-null placeholders and they are 
     *replaced with the same values in the tradeValues Object array.
     */
    String targetName = "NPC Name";
    String itemName = "Item Name";
    int itemQuantity = 1;
    int minCoins = 1;
    int minBuy = 1;
    boolean stackable = false;

    Object[] tradeValues = shop.defaultValues;

    for (int i = 0; i < tradeValues.length; i++) {
        if(String.class.isInstance(tradeValues[i])) {//String check
            if(i==0) { //0 is NPC Name
                targetName = (String) tradeValues[i];
            } else if (i==1) { //1 is Item Name
                itemName = (String) tradeValues[i];
            }
        } else if (Integer.class.isInstance(tradeValues[i])) { //Int check
            if(i==2) { //2 is Item Quantity
                itemQuantity = (Integer) tradeValues[i];
            } else if (i==3) { //3 is Minimum coins
                minCoins = (Integer) tradeValues[i];
            } else if (i==4) { //4 is the Minimum Buy limit
                minBuy = (Integer) tradeValues[i];
            }
        } else if (Boolean.class.isInstance(tradeValues[i])) { //Bool check
                stackable = (Boolean) tradeValues[i]; //5 is the item Stackable 
        } else {
            //TODO: Implement exception
        }
    }

    //Calls ShopTrader() method shopTrader
    ShopTrader trade = new ShopTrader();
    trade.shopTrader(targetName, itemName, itemQuantity, minCoins, minBuy, worldHop, stackable);
}

I feel like using a for loop like this is not the correct way for me to be looping through these Objects, I shouldn't have to check i== for each variable. 我觉得使用这样的for循环对我来说不是遍历这些对象的正确方法,我不必为每个变量检查​​i ==。

Also it hinders me from adding overloads to the shopTrader method as I would have to write an entirely new for loop for each overload. 这也阻碍了我向shopTrader方法添加重载,因为我必须为每个重载编写一个全新的for循环。

Does anyone have a more elegant solution for getting the variables from this array? 有谁有一个更优雅的解决方案来从此数组中获取变量?

I think that instead of storing all of your information in Object[], you may want to create a new class to act as a data structure ie 我认为,与其将所有信息存储在Object []中,您可能想要创建一个新类来充当数据结构,即

public class TradeValue {
    String targetName;
    int itemQuantity;
    // etc.

    String getTargetName() {
        return targetName;
    }

    String getItemQuantity() {
        return itemQuantity;
    }
    // etc
}

You can then just access the information directly 然后,您可以直接访问信息

TradeValue defaultValues = shop.defaultValues;
String targetName = defaultValues.getTargetName();
int itemQuantity = defaultValues. getItemQuantity();
...

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

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