Java converts object [] array to vector

What is 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 []

Thank you, rayt

I should clarify my question

My target platform is BlackBerry

Collection is not supported Array. Aslist() is not, or:/

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

Solution

return new Vector(Arrays.asList(elements));
return new Vector(Arrays.asList(elements));

Now, it may look like copying data twice, but you're not You get a small temporary object (from the aslist list), but this provides a view of the array Instead of copying it, the read and write operations will go to the original array

You can extend the vector and stamp out its protected fields This will give a simpler way to make the vector a view of the array, like arrays Aslist is the same Alternatively, simply copy the data into the field For java me, this is as good as writing an explicit loop Unverified Code:

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

Of course, you may be better than a list than a vector 1.4 the end of service life has been completed Even though 1.5 has completed most of eosl

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>