Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I'm using an arrayList that I'm adding entries from a database via a loop to. Within that loop, I fill an array with the fields from the row that I'm reading. I assign this array to the arrayList.

I understand from googling today that when I call arrayList.add() I'm passing a reference to the array instead of copying the array into the arrayList element. This is not what I want.

What's a better way to go about this?

    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++;
        }
share|improve this question
2  
Add a copy of sleepItem array. – Rohit Jain Oct 19 '13 at 17:03
    
Both answers given are correct. Marked the one as completed that I liked best. Turns out that I had a different bug in my code that made me discard the solution initially; I looked into it more deeply after ya'll confirmed that that's the right approach. Much appreciated, folks! – Jan Kuboschek Oct 19 '13 at 18:25

2 Answers 2

up vote 3 down vote accepted

This seems to be the gist of your problem

// 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?

What you are asking does not (literally) make sense. In Java, it does not sense to say that you want something to contain an array rather a reference to an array. The array and the reference are fundamentally the same thing as far as the language is concerned.

(From a representational perspective, the "thing" that is in the ArrayList is always going to be a reference to an array. The list uses a backing array which is an array of references. It doesn't somehow internalize the argument array that you "add".)


Actually, what I think you are really asking is how to make each element in the list a reference to a different array. The answer to that is to allocate a new array on each array iteration. Or you could do this:

    sleepCollection.add(sleepItem.clone());
share|improve this answer
String[] sleepItem = new String[7];

must be moved to the loop inner, else entries of your arraylist will contain the same row data ;-).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.