【问题标题】:List of heterogenenous array异构数组列表
【发布时间】:2016-06-09 12:45:37
【问题描述】:

我是“Java 菜鸟”,所以请耐心等待 :)

我需要创建一个包含异构字段的特定结构。

我从 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

我不知道“行”的数量,因为它取决于一些变量(如时间、路线等)。

用 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 ,一些 int 和一些字符串,对吗?
  • 是的,我愿意。我想创建一个具有 Location、string、string、int、int 作为字段的新类“LocationPlus”,然后我将创建一个 LocationPlus 的 ArrayList。你怎么看?
  • 锆石为你做的,我做的,你可以用那个

标签: java android arrays gps location


【解决方案1】:

我认为您最好的选择是创建一个包含所有这些字段(包括Location)的类,然后为它们使用合适的Collection,例如ArrayList

List<LocationData> locations = new ArrayList<>();

然后在您的自定义 LocationData 类中使用 getter/setter 对来获取您想要存储的每个字段。

以自定义类为例:

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

【讨论】:

  • 是什么让您认为 OP 需要二传手?从描述来看,我认为这个类应该是不可变的。吸气剂是的,但我认为二传手没有理由。
  • 我在您写答案时更新了我的问题。它们似乎是相同的解决方案:) 我在主要方法中的使用是否正确?
  • @Ray 我只是习惯于为大多数东西制作 Java bean。如果不需要更改数据,那么,是的,不需要设置器。 Domenico,您可以在任何需要的地方使用对象和列表,包括 main 方法。
【解决方案2】:

我建议使用ArrayList。这基本上是一个数组,(您可以使用它来存储信息)但未设置元素的数量。这意味着您不需要知道“行”的数量。您可以创建一个类型为另一个数组的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

}

【讨论】:

  • 我更喜欢 Zircon 解决方案,但还是谢谢你 :)
猜你喜欢
  • 2016-11-22
  • 1970-01-01
  • 2021-07-18
  • 2017-12-23
  • 1970-01-01
  • 2018-08-03
  • 2017-11-05
  • 2021-10-06
  • 1970-01-01
相关资源
最近更新 更多