简体   繁体   English

Java和数据结构(Map,Hashmap,List,Arraylist等)

[英]Java and data structures (Map, Hashmap, List, Arraylist, …)

I'm new to Java and Android development and I'm stuck on following problem. 我是Java和Android开发的新手,但仍遇到以下问题。 My app gets some data from a database which I would like to structure in a following manner: 我的应用程序从数据库中获取一些数据,我希望通过以下方式进行结构化:

DATA = 
{ 
  RESULT[0] = 
  {
    ARRAY[0]=
    {
      VALUE[0], ..., VALUE[X]
    },
    ARRAY[1] = 
    {
      VALUE[0], ..., VALUE[Y]
    },
    ...,
    ARRAY[N] = 
    {
      VALUE[0], ..., VALUE[Z]
    }
  },
  RESULT[1] =
  {
    /* Same as above */
  },
  ...,
  RESULT[M] =
  {
    /* Same as above */
  }
}

where X, Y, ..., Z can be different in each ARRAY of every RESULT but N is always same for all RESULT . 其中X, Y, ..., Z在每个RESULT每个ARRAY中可以不同,但​​是N对于所有RESULT始终相同。 I would like to access DATA so that I could read ARRAY s from above structure as 我想访问DATA以便我可以从上述结构读取ARRAY

ListArray<String> array = readData(indexOfResult,indexOfArray);

where indexOfResult is 0...M and indexOfArray is 0...N. 其中indexOfResult为0 ... M, indexOfArray为0 ... N。

I have tried this 我已经试过了

Map<Integer,List<Map<Integer,ArrayList<String>>>> data
 = new HashMap<Integer,List<Map<Integer,ArrayList<String>>>>();

but maybe something simpler could do the job? 但也许更简单的方法可以胜任?

You're better of wrapping each level of your object structure in a class that represents it. 最好将对象结构的每个级别包装在代表它的类中。 For example: 例如:

  • The class Data contains an array (or arraylist) of Result classes. Data类包含一个Result类的数组(或arraylist)。
  • Result contains an array (or arraylist) of ValueCollection , which contains the values. Result包含ValueCollection的数组(或arraylist),其中包含值。

It's just a raw suggestion. 这只是一个原始的建议。 When facing this kind of situations always try to define classes that represent the different levels of the big picture, starting from the main container and going further to the tiny bits of data that can be represented with collections or fields. 面对这种情况时,请始终尝试定义代表全局各个级别的类,这些类从主容器开始,再到可以用集合或字段表示的微小数据位。

Think of a design yourself (I always recommend pen & paper if you can't come up with one directly on code) and try it out. 自己想一想(如果您不能直接在代码上提出建议,我总是建议使用笔和纸)并尝试一下。 It's the best way to learn. 这是最好的学习方式。

you should probably create some classes that represent what you are trying to display instead of parametrizising everything. 您可能应该创建一些类来表示您要显示的内容,而不是对所有内容进行参数化。

eg 例如

class Example implements Map<Integer, List<String>> {
...
}

Or if you want to do it your way: 或者,如果您想按照自己的方式做:

Map<Integer,List<Map<Integer,ArrayList<String>>>> data = new HashMap<Integer,List<Map<Integer,ArrayList<String>>>>();

List<Map<Integer,ArrayList<String>>> list1 = new Arraylist<Map<Integer,ArrayList<String>>>();
Map<Integer,ArrayList<String>> map1 = new HashMap<Integer,ArrayList<String>>();
ArrayList<String>> list2 = new ArrayList<String>>();

list2.add("test");
map1.add(1, list2);
list1.add(map1);

data.put(1, list1);

Then you have to write a method where you get this data out of your data structure. 然后,您必须编写一种从数据结构中获取数据的方法。 But you should always try to code as simple as possible and structure everything so it is easy to understand. 但是,您应该始终尝试编写尽可能简单的代码并组织所有内容,以便于理解。

you have some options: 您有一些选择:

Map<Integer,List<String[]> result;

or 要么

List<List<String[]> result;

or 要么

List<List<List<String>>> result;

or even 甚至

String[][][] result;

...depends on your usage and what you prefer. ...取决于您的使用情况和您的喜好。

I got my problem solved with 我解决了我的问题

SparseArray<SparseArray<ArrayList<String>>>

However, I encountered another issue which I just can't understand. 但是,我遇到了另一个我无法理解的问题。 I do following 我照做

/* MAIN */

Data data;

String s = "";
ArrayList<String> arr_N = new ArrayList<String>();
arr_N.add(s);
data.addResult(arr_N);

and in class Data I have 在课堂上我有

/* CLASS DEFINITION */

class Data {

  private final static int N_A = 0;
  private SparseArray<SparseArray<ArrayList<String>>> results;
  private int n_results;

  public Data() {
    this.results = new SparseArray<SparseArray<ArrayList<String>>>();
    this.n_results = 0;
  }

  public void addResult(ArrayList<String> a) {
    SparseArray<ArrayList<String>> result = new SparseArray<ArrayList<String>>(1);
    result.put(N_A,a);
    this.results.put(this.n_results,result);
    this.n_results++;
  }

  public ArrayList<String> getA_N(int i) {
    return( this.get_value(i,N_A) );
  }

  private ArrayList<String> get_value(int i, int j) {
    SparseArray<ArrayList<String>> result = this.results.valueAt(results.keyAt(i));
    ArrayList<String> list = result.valueAt(result.keyAt(j));
    return( list );
  }
}

Now, if I do this 现在,如果我这样做

/* MAIN */

int i = 0; /* Something that is < n_results in class Data */
ArrayList<String> r = data.getA_N(i);
int n_r = r.size();
System.out.println("Size of a_n is " + r.size() );

I get "Size of a_n is 1". 我得到“ a_n的大小为1”。

Q1: In the beginning I set s = ""; Q1:一开始我设置s = ""; but is it so that this is not treated as an empty string? 但这是否不将其视为空字符串?

Q2: If I do r.isEmpty() I get that r is not an empty string (although I set s = ""; ). Q2:如果我做r.isEmpty()我会得到r不是一个空字符串(尽管我将s = ""; )。 Why is that? 这是为什么?

Q3: If I set s = null in the beginning, I still get that the size of r above (In MAIN) is 1, even though r.get(i) (where 0 <= i < n_results ) gives me Index out of bounds exception. Q3:如果我在开始时将s = null设置,即使r.get(i) (其中0 <= i < n_results )为我提供了索引,我仍然得到上面r (在MAIN中)的大小为1。边界异常。 Why is that? 这是为什么?

Ok, I think I should not put a into result in addResult if it is "" , null or something funny. 好吧,我想我不应该把aresultaddResult如果是""null或一些有趣的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM