views:

2280

answers:

6

What's the best way to convert an Object array to a Vector?

JDE < 1.5

public Vector getListElements()
{
  Vector myVector = this.elements;
  return myVector;
}

this.elements is an Object[]

Thanks, rAyt

I should clarify my question

My target platform is a blackberry.

Collections aren't supported. Array.asList() isn't, either :/

Full Class

package CustomElements;

import net.rim.device.api.ui.component .*;
import net.rim.device.api.collection.util.*; 
import net.rim.device.api.util.*;
import java.util.*;

public class ContactsList extends SortedReadableList implements KeywordProvider
{
    // Constructor
    public ContactsList(Vector contacts)
    {
     super(new ContactsListComparatorByFirstName());    
     loadFrom(contacts.elements());    
    }
    // Add Element to ContactsSortedReadableList
    void addElement(Object element)
    {
     doAdd(element); 
    }   

    public Vector getListElements()
    {
     return new Vector(Collection


     Vector test = this.getElements();
    }
    // getKeywords
    public String[] getKeywords(Object element) 
    {
     return StringUtilities.stringToWords(((Contact)element).get_contactFirstName());
        // return StringUtilities.stringToWords(element.toString());
    }  
    //  Comparator sorting Contact objects by name
    final static class ContactsListComparatorByFirstName implements Comparator
    {                           
     public int compare(Object o1, Object o2)
     {
      // Sticky Entries Implementation
      if(((ContactsListObject)o2).getSticky())
      {
       return 1;
      } else
       if (((ContactsListObject)o1).getSticky())
       {
        return -1;
       } else
       {
        if(((ContactsListObject)o1).get_contactFirstName().compareTo(((ContactsListObject)o2).get_contactFirstName()) <0)
        {
         return -1;
        }
        if(((ContactsListObject)o1).get_contactFirstName().compareTo(((ContactsListObject)o2).get_contactFirstName()) >0)
        {
         return 1;
        }
        else
        {
         return 0;
        }
       }
     }        
    }    
}
+1  A: 

1) Copy the array elements to the Vector, or

2) Use Arrays.asList(...); to return a List, which isn't exactly a Vector, but you should be coding the List interface anyway.

camickr
So, no way around a for loop?
Henrik P. Hessel
I suggest option 2 except it is worth noting that this does not take a copy of the array, it only wraps it. I wouldn't suggest option 1 i.e. don't use a vector unless you really need to.
Peter Lawrey
@rAyt: Even if there was a built-in function to do so, it would still use a for-loop behind the scenes. There is no magic :)
Adam Bernier
`System.arraycopy` is magic!
Tom Hawtin - tackline
no magic... damn you Research in Motion!
Henrik P. Hessel
+10  A: 
return new Vector(Arrays.asList(elements));

Now, it may look as if you are copying the data twice, but you aren't. You do get one small temporary object (a List from asList), but this provides a view of the array. Instead of copying it, read and write operations go through to the original array.

It is possible to extends Vector and poke its protected fields. This would give a relatively simple way of having the Vector become a view of the array, as Arrays.asList does. Alternatively, just copying data into the fields. For Java ME, this is about as good as it gets without writing the obvious loop. Untested code:

return new Vector(0) {{
    this.elementData = (Object[])elements.clone();
    this.elementCount = this.elementData.length;
}};

Of course, you are probably better off with a List than a Vector. 1.4 has completed its End of Service Life period. Even 1.5 has completed most of its EOSL period.

Tom Hawtin - tackline
+1 The way to go.
Tom
Thanks, Tom
Tom Hawtin - tackline
still a great answer, no reason to vote him down!
Henrik P. Hessel
Wow, second solution looks, just so elegant!
Henrik P. Hessel
Very inventive!
jqno
I think "inventive" needs quotes. :)
Tom Hawtin - tackline
Sorry that can not give you 2 votes for this answer. Thanks!
Antonio
+2  A: 

In J2ME, you're stuck iterating over the array and add the elements one by one.

Vector v = new Vector();
for (int i = 0; i < this.elements.length; i++) {
    v.add(this.elements[i]);
}
jqno
Should I understand why Research in Motion provide something like SortedReadableList which has an LoadFrom Method, but no LoadTO Method?! :)
Henrik P. Hessel
+1  A: 

imho your only viable option is:

public Vector getListElements()
    Vector vector = new Vector(this.elements.length);

    for (int i = 0; i < this.elements.length; i++) {
        vector.add(this.elements[i]);
    } 

    return vector;
}
dfa
Yeah, seems that way. going to suck copying 1000+ objects in the objects array.
Henrik P. Hessel
If you're concerned about it, you could toss in an initialCapacity of elements.length to the Vector constructor.
Carl Manaster
@Carl: fixed, thanks
dfa
+1  A: 

A simplified comparator which does basically the same thing.

final static class ContactsListComparatorByFirstName implements Comparator {
    public int compare(Object o1, Object o2) {
            // Sticky Entries Implementation
        ContactsListObject clo2 = (ContactsListObject) o2;
        ContactsListObject clo1 = (ContactsListObject) o1;
        if (clo2.getSticky()) return 1;
        if (clo1.getSticky()) return -1;
        return clo1.get_contactFirstName().compareTo(clo2.get_contactFirstName());
    }
}

Using generics and ?: it would be just

static final class ContactsListComparatorByFirstName implements Comparator<ContactsListObject> {
    public int compare(ContactsListObject clo1, ContactsListObject clo2) {
        return clo2.getSticky() ? 1 : // Sticky Entries Implementation
            clo1.getSticky() ? -1 :
            clo1.get_contactFirstName().compareTo(clo2.get_contactFirstName());
    }
}

But to answer your question... (oh I see Tom has what I would put already)

Peter Lawrey
Generics aren't supported in my java version, either! But thanks for the hint +1
Henrik P. Hessel
A: 

A for loop. I've seen your comments about trying to avoid this. How do you think some api method is implemented? I mean, even if it involves a native call, it is essentially the same.

Hameltime