简体   繁体   中英

Get ArrayList from Object

I have one method that returns an object with two arraylists:

return new Object[] {work, play};

I am trying to get them back out in another method. I have tried casting to ArrayList but I get the error 'array required, but java.lang.Object found'.

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

Full code for ArrayList creation:

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};
}

return new Object[] {work, play}; i think returns an array of object. Try ArrayList result = new ArrayList(); put work andd play inside result then return result. Then

In your calling code, you should set the reference type of obj as an Object array. You probably have Object obj = getWorkandPlay(anArrayList); in your code. Change it to Object[] obj = getWorkandPlay(anArrayList); .

I am doing something like this based on your code and it works...

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];
    }
}

output

w w. test
p p. test

You probably have a typo somewhere in your code. This compiles for me:

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};
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM