我有两个数组:
SELECT * RECORDS
WHERE date >= date('2017-01-23') AND date <= date('2017-01-24')
ORDER BY DATE,TIME
数组E中的元素如下:float [] E;
float [] Location;
数组位置中的元素为{1900, 16400, 77666, 8000, 13200, 15600}
这些数据是从我们关联的数据库中提取的,意思是:
{Birmingham, Blackburn, London, Luton, Manchester, Newcastle}
我希望能够确保位置与正确的数据相关联,如上所示,如果有意义的话。有没有办法做到这一点?
提前致谢!
答案 0 :(得分:2)
嗯,他们已经通过索引关联了。 E
中索引0处的条目与Location
中索引0处的条目相关。
但是我会通过不来解决这个问题。相反,我有一个存储一个对象的数组,其中(例如)浮动1900(我不会使用float
;至少使用double
)和字符串{ {1}}。
"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...
}
答案 1 :(得分:0)
您可以使用android.util.Pair<F,S>。
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);
}
答案 2 :(得分:0)
只需使用HashMap!
HashMap是一个具有键值的数组。你可以迭代它等等。例如这段代码:
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());
}
祝你好运
答案 3 :(得分: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]));
}
答案 4 :(得分:-1)
是浮点数,与位置相同吗?
你应该为那个
定义一个Pojo类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]);
}
}
}