Search code examples
javaandroidparticle-swarm

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). It is called 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.

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. So if they enter 4 services, then 6 dimensions altogether.Then I want to assign an 'int' value to these services like this...

   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. Any help or advice would be great, I m a uni student undertaking this as a project.

EDIT: I have now implemented a hashmap in the getGoodness() function. 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. 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. 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. 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 :)


Solution

  • As i told in my comment you can use java.util.hashmap for this.

    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:

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