java的对象转换[]数组来矢量 [英] Java Convert Object[] Array to Vector
问题描述
什么是对象数组转换到Vector的最佳方式?
&JDE LT; 1.5
公共向量getListElements()
{
矢量myVector = this.elements;
返回myVector;
}
this.elements是一个Object []
谢谢,
rAyt
我要澄清我的问题
我的目标平台是黑莓。
集合不被支持。 Array.asList()不是,或者:/
全类
包CustomElements;导入net.rim.device.api.ui.component。*;
导入net.rim.device.api.collection.util *。
导入net.rim.device.api.util *。
进口的java.util。*;公共类ContactsList扩展SortedReadableList实现KeywordProvider
{
//构造
公共ContactsList(矢量触点)
{
超级(新ContactsListComparatorByFirstName());
loadFrom(contacts.elements());
}
//添加元素到ContactsSortedReadableList
无效的addElement(Object元素)
{
的doAdd(元);
} 公共向量getListElements()
{
返回新的Vector(集合
向量测试= this.getElements();
}
// getKeywords
公众的String [] getKeywords(Object元素)
{
返回StringUtilities.stringToWords(((联系)元素).get_contactFirstName());
//返回StringUtilities.stringToWords(element.toString());
}
//比较排序的名字联系对象
最终的静态类ContactsListComparatorByFirstName实现比较
{
公众诠释比较(对象01,对象02)
{
//粘项实施
如果(((ContactsListObject)O2).getSticky())
{
返回1;
}其他
如果(((ContactsListObject)01).getSticky())
{
返回-1;
}其他
{
if(((ContactsListObject)o1).get_contactFirstName().compareTo(((ContactsListObject)o2).get_contactFirstName()) ℃,)
{
返回-1;
}
if(((ContactsListObject)o1).get_contactFirstName().compareTo(((ContactsListObject)o2).get_contactFirstName())大于0)
{
返回1;
}
其他
{
返回0;
}
}
}
}
}
返回新的Vector(Arrays.asList(元素));
现在,它可能看起来好像你两次复制数据,但你不是。你会得到一个小小的临时对象(从 asList
A 列表
),但这提供了阵列的景色。而不是复制它,读,写操作通过原来的数组。
有可能延长矢量
和戳它的保护
字段。这将使具有的一个比较简单的方法矢量
成为阵列的视图,为 Arrays.asList
一样。或者,只是将数据复制到田间地头。对于Java ME,这是关于尽善尽美无需编写明显的循环。未测试code:
返回新的Vector(0){{
this.elementData =(对象[])elements.clone();
this.elementCount = this.elementData.length;
}};
当然,你可能使用列表
比矢量
更好。 1.4已完成了它的使用寿命期末。即使1.5已完成了其大部分EOSL时期。
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;
}
}
}
}
}
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.
这篇关于java的对象转换[]数组来矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!