2017-01-23 2 views
0

Ich habe zwei Arrays ein Element in einem Array mit einem Element verbinden:Wie in einem anderen Array (Android Studio)

float [] E; 
float [] Location; 

die Elemente in Array E sind die folgenden: {1900, 16400, 77666, 8000, 13200, 15600}

Die Elemente in Array Lage sind {Birmingham, Blackburn, London, Luton, Manchester, Newcastle}

diese Daten wurden aus der Datenbank extrahiert, wo sie verbunden sind, das heißt:

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

Ich möchte in der Lage sein sicherzustellen, dass die Standorte den richtigen Daten zugeordnet sind, wie ich oben gezeigt habe, wenn das Sinn macht. Gibt es eine Möglichkeit, das zu tun?

Vielen Dank im Voraus!

+0

Verwendung gleiche Indexdaten holen von beiden Arrays –

+2

Kannst du nicht einfach in einem HashMap speichern? Oder habe ich etwas falsch verstanden? – Robert

+0

Sie haben eine 'Location' Klasse. Sie könnten die Daten dort hinzufügen? –

Antwort

2

Nun, sie sind bereits   per Index zugeordnet. Der Eintrag bei Index 0 in E bezieht sich auf den Eintrag bei Index 0 in Location.

Aber ich würde dies durch nicht mit zwei Arrays lösen. Stattdessen hätte ich ein einzelnes Array, das ein Objekt speichert, das (zum Beispiel) sowohl den Float 1900 (ich würde float nicht verwenden; zumindest double verwenden) als auch die Zeichenfolge "Birmingham" hat.

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... 
} 

Dann:

SomeAppropriateName[] info; 
+0

@Addy: Es hängt ganz davon ab, wie die Informationen verwendet werden, die das OP nicht mit uns geteilt hat. Eine 'HashMap' ** kann ** angebracht sein oder nicht. Wenn ich mit Arrays konfrontiert werde, nehme ich normalerweise die Reihenfolge an. –

+0

Und das OP sollte in Betracht ziehen, eine "Liste" anstelle eines Arrays zu verwenden. –

+0

@ Code-Apprentice: Auch hier kommt es auf den Anwendungsfall an. 'List ' ist möglicherweise eine bessere oder schlechtere Wahl als 'SomeApprivateName []'. –

-1

ist ein Schwimmer e, die gleiche wie Standort?

sollten Sie eine Pojo Klasse für diese

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

} 
0

definieren Sie android.util.Pair<F,S> dafür verwenden können.

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

Einfach mit HashMap!

HashMap ist ein Array mit Schlüsselwert. Sie können diesen Code auf etc .. Zum Beispiel iterieren:

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

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])); 
    } 
Verwandte Themen