简体   繁体   English

如何将一个整数值赋给String元素的ArrayList?

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

I have an ArrayList of string elements that the user enters in the MainActivity of my application (Android Studio). 我有一个字符串元素的ArrayList ,用户在我的应用程序的MainActivity (Android Studio)中输入。 It is called ArrayList<String> serviceNames . 它被称为ArrayList<String> serviceNames The service names can be anything like Facebook, Fitness App, Clock App etc. I want to assign an Integer value to each of these user inputs of services as I am working on a fitness function to do with Swarm Particle algorithm. 服务名称可以是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;
    }
    }

I want the num_dimensions to be 2 (WLAN and DATA) plus the number of services the user enters in the main activity. 我希望num_dimensions为2(WLAN和DATA)加上用户在主活动中输入的服务数量。 So if they enter 4 services, then 6 dimensions altogether.Then I want to assign an 'int' value to these services like this... 因此,如果他们输入4个服务,那么总共6个维度。然后我想为这些服务分配一个'int'值,就像这样......

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

When that is done, I want to pass it onto the getGoodness() function which will take these values as bits in the number of dimensions, so Facebook will be the 3rd bit in the dimensions. 完成后,我想将它传递给getGoodness()函数,该函数将这些值作为维数的位,因此Facebook将成为维度中的第3位。 Any help or advice would be great, I ma uni student undertaking this as a project. 任何帮助或建议都会很棒,我作为一个项目,我是一名学生。

EDIT: I have now implemented a hashmap in the getGoodness() function. 编辑:我现在已经在getGoodness()函数中实现了一个hashmap。 My code now looks like this... 我的代码现在看起来像这样......

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

I need the first 2 bits of NUM_DIMENSIONS to be WLAN and DATA. 我需要NUM_DIMENSIONS的前两位是WLAN和DATA。 So for each service the user enters, the integer value assigned to them goes up by 1 starting at 2. So... (2, Facebook), (3, Twitter), (4, Uber) etc. Now my next problem is, passing these down to the getGoodness() function, I have an arraylist from the main UI activity that takes a utility(priority) cost from 0-100 of each service. 因此,对于用户输入的每个服务,分配给它们的整数值从2开始增加1.所以......(2,Facebook),(3,Twitter),(4,优步)等等。现在我的下一个问题是,将这些传递给getGoodness()函数,我有一个来自主UI活动的arraylist,它从每个服务的0-100中获取实用程序(优先级)成本。 This is called costUtilities, I want the bits that contain a utility of 80.0-100.0 (double) to return me a value of -2000 if it isn't on. 这称为costUtilities,我想要包含80.0-100.0(double)实用程序的位,如果它没有打开,则返回值-2000。 If you can help with this, that would be great, if not, thank you for the help you've given so far, and wish me luck in my final year individual project on PSO :) 如果你可以帮忙解决这个问题,那就太棒了,如果没有,谢谢你迄今给予的帮助,祝我在PSO的最后一年个人项目中好运:)

As i told in my comment you can use java.util.hashmap for this. 正如我在评论中所说,你可以使用java.util.hashmap

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

or if you have taken services as an arraylist serviceNames then you can put it afterwards: 或者如果您已将服务作为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); 
}

Rajat answer is correct. 拉贾特的回答是正确的。 You can also creat a JSON Object, and insert the labels that you want, and after that include it in the hashMap. 您还可以创建一个JSON对象,并插入所需的标签,然后将其包含在hashMap中。

JSON example: 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