Java 将 Object[] 数组转换为向量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1116636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Convert Object[] Array to Vector
提问by Henrik P. Hessel
What's the best way to convert an Object array to a Vector?
将对象数组转换为向量的最佳方法是什么?
JDE < 1.5
JDE < 1.5
public Vector getListElements()
{
Vector myVector = this.elements;
return myVector;
}
this.elements is an Object[]
this.elements 是一个对象[]
Thanks, rAyt
谢谢,雷特
I should clarify my question
我应该澄清我的问题
My target platform is a blackberry.
我的目标平台是黑莓。
Collections aren't supported. Array.asList() isn't, either :/
不支持集合。Array.asList() 也不是:/
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;
}
}
}
}
}
采纳答案by Tom Hawtin - tackline
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.
现在,看起来好像您将数据复制了两次,但实际上并非如此。您确实会得到一个小的临时对象(List
来自asList
),但这提供了数组的视图。读取和写入操作不会复制它,而是通过原始数组进行。
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:
可以扩展Vector
和戳它的protected
领域。这将提供一种相对简单的方法,使Vector
成为数组的视图,就像Arrays.asList
这样。或者,只需将数据复制到字段中。对于 Java ME,这与无需编写明显循环一样好。未经测试的代码:
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.
当然,使用 a 可能List
比使用更好Vector
。1.4 已完成其使用寿命终止期。甚至 1.5 也已经完成了大部分 EOSL 周期。
回答by camickr
Copy the array elements to the
Vector
, orUse
Arrays.asList(...)
to return aList
, which isn't exactly aVector
, but you should be coding theList
interface anyway.
将数组元素复制到
Vector
, 或使用
Arrays.asList(...)
返回一个List
,这不完全是Vector
,但你应该编码List
反正接口。
回答by jqno
In J2ME, you're stuck iterating over the array and add the elements one by one.
在 J2ME 中,您必须遍历数组并一个一个地添加元素。
Vector v = new Vector();
for (int i = 0; i < this.elements.length; i++) {
v.add(this.elements[i]);
}
回答by dfa
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;
}
回答by Peter Lawrey
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)
但是要回答你的问题......(哦,我看到汤姆已经有了我想说的)
回答by Klaus Engelhardt
A reasonably concise way to do it is something like:
一个相当简洁的方法是这样的:
Object[] xx = { 1, "cat", new Point(100,200) };
Vector vv = new Vector(Arrays.asList(xx));
System.out.println("vv=="+vv.toString());
But y'all knew that already, I guess.
但我想你们都已经知道了。