1

我有一种方法可以返回一个带有两个数组列表的对象:

return new Object[] {work, play};

我正试图用另一种方法让他们回来。我曾尝试强制转换为 ArrayList,但我收到错误“需要数组,但找到了 java.lang.Object”。

ArrayList setWork = (ArrayList)obj[0];
ArrayList setPlay = (ArrayList)obj[1];

ArrayList 创建的完整代码:

public static Object[] getWorkandPlay(ArrayList al) {

    ArrayList work = new ArrayList();
    ArrayList play = new ArrayList();

    for (int i=0; i<al.size(); i++){
        String item = (String) al.get(i);

        if (item.startsWith("w.")) {
            System.out.println("w " + item);
            work.add(item);
        } else if (item.startsWith("p.")) {
            System.out.println("p " + item);
            play.add(item);
        } else {
            System.out.println("Entries must start with either w. or p.\n");
        }
    }
    return new Object[] {work, play};
}
4

4 回答 4

1

返回新对象 [] {工作,游戏};我认为返回一个对象数组。尝试 ArrayList 结果 = new ArrayList(); 将工作和播放放在结果中,然后返回结果。然后

于 2012-06-02T00:09:17.530 回答
1

在您的调用代码中,您应该将 obj 的引用类型设置为 Object 数组。你的代码中可能有 Object obj = getWorkandPlay(anArrayList);。将其更改为Object[] obj = getWorkandPlay(anArrayList);.

于 2012-06-02T00:15:05.747 回答
1

我正在根据您的代码做这样的事情,它可以工作......

class Test{
    public static Object[] getWorkandPlay(ArrayList al) {
        ArrayList work = new ArrayList();
        ArrayList play = new ArrayList();

        for (int i=0; i<al.size(); i++){
            String item = (String) al.get(i);

            if (item.startsWith("w.")) {
                System.out.println("w " + item);
                work.add(item);
            } else if (item.startsWith("p.")) {
                System.out.println("p " + item);
                play.add(item);
            } else {
                System.out.println("Entries must start with either w. or p.\n");
            }
        }
        return new Object[] {work, play};
    }
    public static void main(String[] args) {
        ArrayList<String> al=new ArrayList<>();
        al.add("w. test");
        al.add("p. test");
        Object[] obj=getWorkandPlay(al);
        ArrayList setWork = (ArrayList)obj[0];
        ArrayList setPlay = (ArrayList)obj[1];
    }
}

输出

w w. test
p p. test
于 2012-06-02T00:19:53.177 回答
1

您的代码中的某处可能有错字。这为我编译:

import java.util.*;

public class SampleClass {

    public static void main(String[] args) {
        // Create an ArrayList and add some sample Strings
        ArrayList al = new ArrayList();
        al.add("w. test");
        al.add("p. test");

        Object[] obj = getWorkandPlay(al);

        ArrayList setWork = (ArrayList)obj[0];
        ArrayList setPlay = (ArrayList)obj[1];
    }

    public static Object[] getWorkandPlay(ArrayList al) {
        ArrayList work = new ArrayList();
        ArrayList play = new ArrayList();

        for (int i=0; i<al.size(); i++){
            String item = (String) al.get(i);

            if (item.startsWith("w.")) {
                System.out.println("w " + item);
                work.add(item);
            } else if (item.startsWith("p.")) {
                System.out.println("p " + item);
                play.add(item);
            } else {
                System.out.println("Entries must start with either w. or p.\n");
            }
        }

        return new Object[] {work, play};
    }
}
于 2012-06-02T00:20:15.820 回答