异构数组列表
List of heterogenenous array
我是"Java noob",所以请耐心等待:)
我需要创建一个包含异构字段的特定结构。
我正在从 GPS 设备(我的智能手机)获取 Android.Location
,我想存储所有位置,但我需要向每个位置添加一些其他字段。所以,我的结构将是这样的:
[0]: Location - String - int - String - int - String - String
[1]: Location - String - int - String - int - String - String
[2]: Location - String - int - String - int - String - String
[3]: Location - String - int - String - int - String - String
[4]: Location - String - int - String - int - String - String
...
[n]: Location - String - int - String - int - String - String
我不知道 "rows" 的数量,因为它取决于一些变量(如时间、路线等)。
在 Java 中哪种方法最好?
更新
这个解决方案正确吗?
public Class LocationPlus {
private Location location;
private String string1;
private int int1;
private String string2;
private int int2;
// Constructor, setters, getters
}
然后,在我的主要:
List<LocationPlus> locationPlus = new ArrayList<LocationPlus>();
locationPlus.add(new LocationPlus(location, “marco”, 1, “bianco”, 2));
locationPlus.add(new LocationPlus(location, “luca”, 3, “arancio”, 4));
locationPlus.add(new LocationPlus(location, “giovanni”, 5, “rossi”, 6));
我认为您最好的选择是创建一个包含所有这些字段(包括 Location
)的 class,然后为它们使用合适的 Collection
,例如ArrayList
.
List<LocationData> locations = new ArrayList<>();
然后在您的自定义 LocationData
class 中使用 getter/setter 对来获取您想要存储的每个字段。
自定义class,例如:
public class LocationData {
//Name these appropriately! I don't know what they're for.
private Location location;
private String string1;
private int num1;
private String string2;
private int num2;
private String string3;
private String string4;
//Constructor
public LocationData(Location loc, String s1, int n1, String s2, int n2, String s3, String s4) {
location = loc;
//And so on for each field
...
}
//One pair of methods for each field
public Location getLocation() {
return location;
}
public void setLocation(Location loc) {
location = loc;
}
我建议使用 ArrayList
。这基本上是一个数组(您可以使用它来存储信息)但未设置元素的数量。这意味着您不需要知道 "rows" 的数量。您可以创建类型为另一个数组的 ArrayList
。这个数组可以是Object
的类型来存储不同类型的变量。
示例:
ArrayList<Object[]> information = new ArrayList<Object[]>();
// The Object[] within the '<>' is the type of the ArrayList
// In this case, the type of the ArrayList happens to be another
// array. This array has a type of Object, meaning almost
// anything can be stored in it.
public void updateInformation(Location loc, String str1, int num1, String str2, int num2, String str3, String str4) {
int currentSize = 0; // The current size of the ArrayList
currentSize = information.size(); // Returns the size of the ArrayList
// Create a new Object array with a size of seven since
// you have seven pieces of information to store.
information.add(new Object[7]);
// For the following, get the newly created Object array,
// and store your information within the Object array's seven
// indexes.
information.get(currentSize)[0] = loc; // Index 0 of Object Array
information.get(currentSize)[1] = loc; // Index 1 of Object Array
information.get(currentSize)[2] = loc; // Index 2 of Object Array
information.get(currentSize)[3] = loc; // Index 3 of Object Array
information.get(currentSize)[4] = loc; // Index 4 of Object Array
information.get(currentSize)[5] = loc; // Index 5 of Object Array
information.get(currentSize)[6] = loc; // Index 6 of Object Array
// Only go to index six. This is because array SIZES start at 1, but
// array INDEXES start at 0. This means the max index of an array will
// always be one less than the size.
}
// The index referring to the index of the ArrayList of the set of
// desired information.
public Object[] getInformation(int index) {
return information.get(index);
// Return the object array with the information
}
我是"Java noob",所以请耐心等待:)
我需要创建一个包含异构字段的特定结构。
我正在从 GPS 设备(我的智能手机)获取 Android.Location
,我想存储所有位置,但我需要向每个位置添加一些其他字段。所以,我的结构将是这样的:
[0]: Location - String - int - String - int - String - String
[1]: Location - String - int - String - int - String - String
[2]: Location - String - int - String - int - String - String
[3]: Location - String - int - String - int - String - String
[4]: Location - String - int - String - int - String - String
...
[n]: Location - String - int - String - int - String - String
我不知道 "rows" 的数量,因为它取决于一些变量(如时间、路线等)。
在 Java 中哪种方法最好?
更新
这个解决方案正确吗?
public Class LocationPlus {
private Location location;
private String string1;
private int int1;
private String string2;
private int int2;
// Constructor, setters, getters
}
然后,在我的主要:
List<LocationPlus> locationPlus = new ArrayList<LocationPlus>();
locationPlus.add(new LocationPlus(location, “marco”, 1, “bianco”, 2));
locationPlus.add(new LocationPlus(location, “luca”, 3, “arancio”, 4));
locationPlus.add(new LocationPlus(location, “giovanni”, 5, “rossi”, 6));
我认为您最好的选择是创建一个包含所有这些字段(包括 Location
)的 class,然后为它们使用合适的 Collection
,例如ArrayList
.
List<LocationData> locations = new ArrayList<>();
然后在您的自定义 LocationData
class 中使用 getter/setter 对来获取您想要存储的每个字段。
自定义class,例如:
public class LocationData {
//Name these appropriately! I don't know what they're for.
private Location location;
private String string1;
private int num1;
private String string2;
private int num2;
private String string3;
private String string4;
//Constructor
public LocationData(Location loc, String s1, int n1, String s2, int n2, String s3, String s4) {
location = loc;
//And so on for each field
...
}
//One pair of methods for each field
public Location getLocation() {
return location;
}
public void setLocation(Location loc) {
location = loc;
}
我建议使用 ArrayList
。这基本上是一个数组(您可以使用它来存储信息)但未设置元素的数量。这意味着您不需要知道 "rows" 的数量。您可以创建类型为另一个数组的 ArrayList
。这个数组可以是Object
的类型来存储不同类型的变量。
示例:
ArrayList<Object[]> information = new ArrayList<Object[]>();
// The Object[] within the '<>' is the type of the ArrayList
// In this case, the type of the ArrayList happens to be another
// array. This array has a type of Object, meaning almost
// anything can be stored in it.
public void updateInformation(Location loc, String str1, int num1, String str2, int num2, String str3, String str4) {
int currentSize = 0; // The current size of the ArrayList
currentSize = information.size(); // Returns the size of the ArrayList
// Create a new Object array with a size of seven since
// you have seven pieces of information to store.
information.add(new Object[7]);
// For the following, get the newly created Object array,
// and store your information within the Object array's seven
// indexes.
information.get(currentSize)[0] = loc; // Index 0 of Object Array
information.get(currentSize)[1] = loc; // Index 1 of Object Array
information.get(currentSize)[2] = loc; // Index 2 of Object Array
information.get(currentSize)[3] = loc; // Index 3 of Object Array
information.get(currentSize)[4] = loc; // Index 4 of Object Array
information.get(currentSize)[5] = loc; // Index 5 of Object Array
information.get(currentSize)[6] = loc; // Index 6 of Object Array
// Only go to index six. This is because array SIZES start at 1, but
// array INDEXES start at 0. This means the max index of an array will
// always be one less than the size.
}
// The index referring to the index of the ArrayList of the set of
// desired information.
public Object[] getInformation(int index) {
return information.get(index);
// Return the object array with the information
}