2013-10-19 57 views
0

我正在使用arrayList,我通过循环将数据从数据库添加到。在那个循环中,我使用我正在阅读的行中的字段填充数组。我将这个数组分配给arrayList。将数组复制到ArrayList元素

我明白从今天googling,当我呼吁arrayList.add()我传递数组的引用,而不是将数组复制到arrayList元素。这不是我想要的。

什么是更好的方式去做这件事?

Cursor cursor = this.managedQuery(Record.Records.CONTENT_URI, projection,  Record.Records.START_TIME + " > " + 1, null, Record.Records.START_TIME + " ASC"); 

    int i, n, x = 0; 
    List<String[]> sleepCollection = new ArrayList<String[]>(); 
    String[] sleepItem = new String[7]; 

    for (i=0;i<cursor.getCount();i++) 
    { 
     if (i==0) 
      cursor.moveToFirst(); 

     for (n=0;n<5;n++) 
      sleepItem[n] = cursor.getString(n); 

     // hours, duration 
     sleepItem[5] = String.valueOf((((Long.valueOf(sleepItem[1])-Long.valueOf(sleepItem[0]))/1000)+(Long.valueOf(sleepItem[4])*60))/60/60); 
     // minutes, duration 
     sleepItem[6] = String.valueOf((((Long.valueOf(sleepItem[1])-Long.valueOf(sleepItem[0]))/1000)+(Long.valueOf(sleepItem[4])*60))/60%60); 

     if ((x > 0) && 
      (Long.valueOf(sleepCollection.get(x-1)[1]) - Long.valueOf(sleepItem[0]) <= 7200000) // record started within 2 hours if end of previous record 
      ) 
     { 
      // set rating 
      sleepCollection.get(x-1)[2] = String.valueOf((Float.valueOf(sleepCollection.get(x-1)[2])) + Float.valueOf(sleepItem[2])/2); 

      sleepCollection.get(x-1)[1] = sleepItem[1]; // set previous record's end date equal to current record's end date 
      // then add duration of previous record to current record (hours, then minutes) 
      sleepCollection.get(x-1)[5] = String.valueOf(Integer.valueOf(sleepItem[5])+Integer.valueOf(sleepCollection.get(x-1)[5])); 
      sleepCollection.get(x-1)[6] = String.valueOf(Integer.valueOf(sleepItem[6])+Integer.valueOf(sleepCollection.get(x-1)[6])); 
      // check if minutes are 60 or over 
      if (Integer.valueOf(sleepCollection.get(x-1)[6]) >= 60) 
      { 
       // if so, subtract 60 from minutes 
       sleepCollection.get(x-1)[6] = String.valueOf(Integer.valueOf(sleepCollection.get(x-1)[6]) - 60); 
       // and add an hour to hours 
       sleepCollection.get(x-1)[5] = String.valueOf(Integer.valueOf(sleepCollection.get(x-1)[5]+1)); 
      } 
     } 
     else 
     { 
      // this passes a reference of sleepItem. I want sleepCollection to actually contain the array and not a reference to it. What's the best way to do that? 
      sleepCollection.add(sleepItem); 
      x++; 
     } 
+2

添加'sleepItem'数组的副本。 –

+0

给出的两个答案都是正确的。标记完成,我最喜欢的一个。事实证明,我的代码中有一个不同的错误,最初让我放弃了解决方案;在你确认这是正确的方法后,我更深入地研究了它。非常感谢,伙计们! –

回答

3

这似乎是你的问题的要点

// This passes a reference of sleepItem. I want sleepCollection to 
// actually contain the array and not a reference to it. What's 
// the best way to do that? 

你所要求不(字面)意义。在Java中,你不想说某个东西要包含数组而不是对数组的引用。就语言而言,数组和参考基本上是相同的。

(从表象的角度来看,ArrayList中的“东西”总是会引用数组。该列表使用的是一个引用数组的backing数组,它不会以某种方式内化参数数组的是“添加”。)


其实,我觉得你真的是如何使每个元素在列表中的不同数组的引用。答案就是在每个数组迭代上分配一个新数组。或者你可以这样做:

sleepCollection.add(sleepItem.clone()); 
1
String[] sleepItem = new String[7]; 

必须移动到你的ArrayList的环内,否则条目将包含相同的行数据;-)。