0

I have two arrays:

float [] E;
float [] Location;

The elements in array E are the following: {1900, 16400, 77666, 8000, 13200, 15600}

The elements in array Location are {Birmingham, Blackburn, London, Luton, Manchester, Newcastle}

These data have been extracted from my database where they are associated, meaning:

Birmingham - 1900
Blackburn - 16400
London - 77666
Luton- 8000
Manchester-13200
Newcastle-15600

I want to be able ensure that the locations are associated to the right data as I have shown above if that makes sense. Is there a way of doing that?

Thanks In Advance!

7
  • use same index to fetch data from both arrays Commented Jan 23, 2017 at 13:43
  • 2
    Can't you just save it in a HashMap? Or did I misunderstood something? Commented Jan 23, 2017 at 13:43
  • 2
    @MuratK.: They don't, they have a poorly-named Location variable. Commented Jan 23, 2017 at 13:44
  • 1
    You can obviously use whatever conventions you want to in your own code, but when asking others for help, please follow at least the main conventions: Variable names and field names do not start with capital letters, class names do. Commented Jan 23, 2017 at 13:48
  • 1
    The given examples for locations are Strings, not floats. Commented Jan 23, 2017 at 13:48

5 Answers 5

2

Well, they're already associated  by index. The entry at index 0 in E is related to the entry at index 0 in Location.

But I would solve this by not having two arrays. Instead, I'd have a single array storing an object, which had (for example) both the float 1900 (I wouldn't use float, though; at least use double) and the string "Birmingham".

class SomeAppropriateName {
    private double distance;
    private String location;

    SomeAppropriateName(double distance, String _location) {
        this.distance = _distance;
        this.location = _location;
    }

    // ...add getters and setters as appropriate...
}

Then:

SomeAppropriateName[] info;
Sign up to request clarification or add additional context in comments.

4 Comments

@Addy: It depends entirely on how the information is being used, which the OP hasn't shared with us. A HashMap may be appropriate, or not. Normally when presented with arrays, I assume order matters.
And the OP should consider using a List instead of an array.
@Code-Apprentice: Again, it depends entirely on the use case. List<SomeAppropriateName> may well be a better, or worse, choice than SomeAppropriateName[].
@Addy I disagree that a HashMap associating indexes would make life easier than a class which directly associates data.
0

You can use android.util.Pair<F,S> for that.

List<Pair<String,Double>> pairs = Arrays.asList(
    new Pair("Birmingham", 1900),
    new Pair("Blackburn", 16400),
    new Pair("London", 77666),
    new Pair("Luton", 8000),
    new Pair("Manchester", 13200),
    new Pair("Newcastle", 15600)
);

for (Pair<String, Double> pair : pairs) {
    Log.d("log", pair.first + " - " + pair.second);
}

Comments

0

Simply with HashMap !

HashMap is an array with key value. You can iterate on it etc.. For example this code :

 HashMap<Integer, String> hmap = new HashMap<Integer, String>();

  /*Adding elements to HashMap*/
  hmap.put(12, "Chaitanya");
  hmap.put(2, "Rahul");
  hmap.put(7, "Singh");
  hmap.put(49, "Ajeet");
  hmap.put(3, "Anuj");

  /* Display content using Iterator*/
  Set set = hmap.entrySet();
  Iterator iterator = set.iterator();
  while(iterator.hasNext()) {
     Map.Entry mentry = (Map.Entry)iterator.next();
     System.out.print("key is: "+ mentry.getKey() + " & Value is: ");
     System.out.println(mentry.getValue());
  }

  /* Get values based on key*/
  String var= hmap.get(2);
  System.out.println("Value at index 2 is: "+var);

  /* Remove values based on key*/
  hmap.remove(3);
  System.out.println("Map key and values after removal:");
  Set set2 = hmap.entrySet();
  Iterator iterator2 = set2.iterator();
  while(iterator2.hasNext()) {
      Map.Entry mentry2 = (Map.Entry)iterator2.next();
      System.out.print("Key is: "+mentry2.getKey() + " & Value is: ");
      System.out.println(mentry2.getValue());
   }

Good Luck

Comments

0
i don't know how you're querying your database, but i assume you're making one query to get both the location and the distance. 

//1. create a model to represent the Location and its corresponding distance
public class LocationDistance {
    private float distance;
    private String location;

    public LocationDistance(float distance, String location) {
        this.distance = distance;
        this.location = location;
    }

    public String getLocation(){
        return this.location;
    } 

    public float getDistance(){
        return this.distance;
    }

    public void setLocation(String loc){
         this.location=loc;
    } 

    public void setDistance(float distance){
         this.distance=distance;
    }
}

   //2. Loop over the two arrays and create a list of LocationDistance
    List<LocationDistance> locationDistance=new ArrayList<LocationDistance>();
    float[] e = { 1900f, 16400f, 77666f, 8000f, 13200f, 15600f };

    String[] location = { "Birmingham", "Blackburn", "London", "Luton", "Manchester", "Newcastle" };

    for(int i=0; i<e.length;i++){
      locationDistance.add(new LocationDistance(e[i],location[i]));
    }

Comments

-1

is e a float, the same as location?

you should define a Pojo class for that

class Pojo {
    private final float e;
    private final String city;

    public Pojo(float e, String city) {
        this.e = e;
        this.city = city;
    }
    public static void main(String[] args) {
        final float[] e = { 1900f, 16400f, 77666f, 8000f, 13200f, 15600f };
        final String[] location = { "Birmingham", "Blackburn", "London", "Luton", "Manchester", "Newcastle" };
        final Pojo[] p = new Pojo[e.length];

        for (int i = 0; i < e.length; i++) {
            p[i] = new Pojo(e[i], location[i]);
        }
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.