0

说我有以下内容:

Class myclass
{
    public string stra ="", strb = ""
    myclass(String a, String b){stra=a;strb=b}
}


//then in the app I want to do:

myclass myclassinst1 = new myclass("blah","xxxx");
myclass myclassinst2 = new myclass("blah2","yyyy");
myclass myclassinst3 = new myclass("blah3","zzzz");

list <myclass> mylist = new ArrayList<myclass>();
mylist.add(myclassinst1 );
mylist.add(myclassinst2 );
mylist.add(myclassinst3 );

//How would I then convert that to a String[] Array of all the stra elements without using a loop.
//eg: 
String[] strarr_a = mylist.toarray(myclass.stra);
String[] strarr_b = mylist.toarray(myclass.strb);


//instead of having to do
String[] strarr_a = new String[mylist.size()];
String[] strarr_b = new String[mylist.size()];
for (int i=0;i<mylist.size();i++)
{
   strarr_a[i] = mylist.get(i).stra;
   strarr_b[i] = mylist.get(i).strb;
}
4

5 回答 5

5

没有简单的方法可以做到这一点,例如使用标准 Java API 方法。您必须自己编写循环。

请注意,您的代码中有许多错误(缺少分号、拼写错误的类名,例如stringand list,以及关键字是class, not Class)。

我会这样写:

class MyClass {
    // Note that it's bad practice to make fields public.
    // Also, you do not need to initialize fields to "" if you're going to initialize them in the constructor.
    private String stra;
    private String strb;

    public MyClass(String a, String b) {
        this.stra = a;
        this.strb = b;
    }

    public String getStra() {
        return stra;
    }

    public String getStrb() {
        return strb;
    }
}

MyClass myclassinst1 = new MyClass("blah","xxxx");
MyClass myclassinst2 = new MyClass("blah2","yyyy");
MyClass myclassinst3 = new MyClass("blah3","zzzz");

List<MyClass> mylist = new ArrayList<MyClass>();
mylist.add(myclassinst1);
mylist.add(myclassinst2);
mylist.add(myclassinst3);

List<String> list1 = new ArrayList<String>();
List<String> list2 = new ArrayList<String>();

for (MyClass obj : mylist) {
    list1.add(obj.getStra());
    list2.add(obj.getStrb());
}

String[] strarrA = list1.toArray(new String[list1.size()]);
String[] strarrB = list2.toArray(new String[list2.size()]);
于 2010-05-14T13:43:43.667 回答
2

有一些库为这样的事情提供了功能包装器。我用过http://functionaljava.org/但这只是个人喜好问题。

import fj.data.List
import fj.F

// Creation of myclassinst1, myclassinst2, myclassinst3 as above

List<myclass> mylist = List.list(myclassinst1, myclassinst2, myclassinst3);

List<String> strarr_a = mylist.map(new F<myclass, String>() {
  String f(myclass c) { return c.stra } 
});

List<String> strarr_b = mylist.map(new F<myclass, String>() {
  String f(myclass c) { return c.strb } 
});

请记住,这些Lists 来自functionaljava,而不是本机 Java Lists。ArrayLists如果需要,它们可以转换为。

于 2010-05-14T14:04:07.050 回答
0

没有内置方法可以将其分解,您必须使用循环手动完成。

(顺便说一句,您的命名约定不是很标准......例如 MyClass、myClassInst1、myList 等会是更好的选择。)

于 2010-05-14T13:44:51.190 回答
0

这将需要一个循环。在 Java 中没有内置功能可以做到这一点。

于 2010-05-14T13:48:09.250 回答
0
class MyClass {
    string a;
    string b;

    //...etc
}

//now pretend you have: Collection<MyClass> myClasses

String[] myAs = (String[]) CommonUtils.collect(myClasses, new Transformer() {
                    public Object transform(Object o) {
                        return ((MyClass)o).getA();
                    }
                }).toArray();

看,谁说你需要不止一行:)?CollectionUtils 是 Apache Commons 的一部分(它很摇滚),而不是核心 Java。

链接:http ://commons.apache.org/collections/apidocs/org/apache/commons/collections/CollectionUtils.html

于 2010-05-14T16:59:10.890 回答