簡體   English   中英

如何將一個整數值賦給String元素的ArrayList?

[英]How to assign an integer value to an ArrayList of String elements?

我有一個字符串元素的ArrayList ,用戶在我的應用程序的MainActivity (Android Studio)中輸入。 它被稱為ArrayList<String> serviceNames 服務名稱可以是Facebook,Fitness App,Clock App等。我想為每個服務的用戶輸入分配一個Integer值,因為我正在使用與Swarm粒子算法相關的適應度函數。

public class CustomServiceSelection implements Goodness {

    public static final int numOfServices = MainActivity.servicenames.size();
    public static final int NUM_DIMENSIONS = 2 + numOfServices;
    public static final int DATA = 0;
    public static final int WLAN = 1; //get the names of the services from the arraylist
    //then convert them into an integer for the bits/no_dimensions


    boolean alwaysOn = MainActivity.costUTILITY.contains(100.0);


    ArrayList<String> serviceNames = MainActivity.servicenames;
    ArrayList<Double> costData = MainActivity.costDATA;
    ArrayList<Double> costWlan = MainActivity.costWLAN;
    ArrayList<Double> costUtilities = MainActivity.costUTILITY;
    private double batteryCost;
    //ArrayList<Double> battery = MainActivity.costBattery;

    public CustomServiceSelection(double batteryCost, ArrayList<Double> costData, ArrayList<Double> costWlan,
                                  ArrayList<Double> costUtilities) {
        if (costUtilities == null || costUtilities.size() < 1 || costData.size() < 1 || costWlan.size() < 1) {
            throw new RuntimeException("Please add atleast 1 cost to Data, WLAN and Utility");
        }
        this.batteryCost = batteryCost; //make sure you add battery field to UI, user enters battery level
        this.costData = costData;
        this.costWlan = costWlan;
        this.costUtilities = costUtilities;
        //this.services = services;
    }

    public double getGoodness(boolean[] bits) {
        double utility = 0.0;
        double rcost = 0.0;
        ArrayList<Double> resourceCost = new ArrayList<Double>();

        for(Double i : costData){

        }

        for (int x = 0; x <= NUM_DIMENSIONS; x++) { //for each resource that is a bit
            if (bits[x] == alwaysOn) {  //if a utility cost of a service is 100, return -2000.0
                return -2000;
            }

            if (bits[DATA] && bits[WLAN]) {
                return -500;
            }
            if (!bits[DATA] || bits[WLAN]) {
                return -450; //particle goodness always returns this??
            }
            if (bits[DATA]) {
                resourceCost = costData;
            } else if (bits[WLAN]) {
                resourceCost = costWlan;
            }

            for (int i = 0; i <= NUM_DIMENSIONS; i++) { //for each service the user enters
                if (bits[i]) {
                    utility += costUtilities.get(i);
                    rcost += resourceCost.get(i);
                }
            }
            if (rcost < batteryCost) {
                return utility;
            }


        }
        return utility * 0.50;
    }
    }

我希望num_dimensions為2(WLAN和DATA)加上用戶在主活動中輸入的服務數量。 因此,如果他們輸入4個服務,那么總共6個維度。然后我想為這些服務分配一個'int'值,就像這樣......

   public static final int Facebook = 2;
   public static final int Twitter = 3;
   public static final int ClockApp = 4;

完成后,我想將它傳遞給getGoodness()函數,該函數將這些值作為維數的位,因此Facebook將成為維度中的第3位。 任何幫助或建議都會很棒,我作為一個項目,我是一名學生。

編輯:我現在已經在getGoodness()函數中實現了一個hashmap。 我的代碼現在看起來像這樣......

    public double getGoodness(boolean[] bits) {
    double utility = 0.0;
    double rcost = 0.0;
    ArrayList<Double> resourceCost = new ArrayList<Double>();

    for(int i = 2; i <= NUM_DIMENSIONS; i++) { //hashmap that stores the   service names with an int value
        for(int k = 0; k <= numOfServices; k++) {
            serviceMap.put(i, serviceNames.get(k));
        }

我需要NUM_DIMENSIONS的前兩位是WLAN和DATA。 因此,對於用戶輸入的每個服務,分配給它們的整數值從2開始增加1.所以......(2,Facebook),(3,Twitter),(4,優步)等等。現在我的下一個問題是,將這些傳遞給getGoodness()函數,我有一個來自主UI活動的arraylist,它從每個服務的0-100中獲取實用程序(優先級)成本。 這稱為costUtilities,我想要包含80.0-100.0(double)實用程序的位,如果它沒有打開,則返回值-2000。 如果你可以幫忙解決這個問題,那就太棒了,如果沒有,謝謝你迄今給予的幫助,祝我在PSO的最后一年個人項目中好運:)

正如我在評論中所說,你可以使用java.util.hashmap

HashMap<int,String> serviceMap = new HashMap<int,String>
//for adding values to a hashmap
serviceMap.put(3,"facebook");

或者如果您已將服務作為arraylist serviceNames,那么您可以在以后添加:

HashMap<int,String> serviceMap = new HashMap<int,String>
for(String serviceName : serviceNames) {
    // you need to replace serviceIntValue with the actual int value of the service
    serviceMap.put(serviceIntValue,serviceName); 
}

拉賈特的回答是正確的。 您還可以創建一個JSON對象,並插入所需的標簽,然后將其包含在hashMap中。

JSON示例:

public static JSONObject constructEHRJSON(int sessionID, String EHR) {
  {
    JSONObject JSONobj = new JSONObject();


    try {

        JSONobj.put("1", sessionID);
        JSONobj.put("2", EHR);
    } catch (JSONException e) {
        /
    }
    return JSONobj;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM