Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Convert Object[] Array to Vector

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;
                    }
                }
        }        
    }    
}
like image 926
Henrik P. Hessel Avatar asked Jul 12 '09 18:07

Henrik P. Hessel


People also ask

What is object [] array in Java?

In the Java programming language, arrays are objects (§4.3. 1), are dynamically created, and may be assigned to variables of type Object (§4.3. 2). All methods of class Object may be invoked on an array. An array object contains a number of variables.

Can we convert object to array in Java?

2. Use Plain Java. We can use a ByteArrayOutputStream and ObjectOutputStream object to serializable an object to a byte array. Note that our User class must implement the Serializable interface.

Why is Vector not used in Java?

This is because Vector synchronizes on each operation and does not synchronize the whole Vector instance itself. This is not desired in real-world applications, where the whole set of operations needs to be synchronized and not individual operations.


2 Answers

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.

like image 200
Tom Hawtin - tackline Avatar answered Oct 04 '22 16:10

Tom Hawtin - tackline


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]);
}
like image 37
jqno Avatar answered Oct 04 '22 16:10

jqno