user164863
user164863

Reputation: 641

Handling arrays of objects in Java

I'm new to Java and used to create arrays and objects in PHP.

Now I wrote a short function in Java for extractions some parts of the text. The function packs up all extracted text pieces into an object and returns to the main code. However whatever I do with the returned result I can't get items back from that.

Here is my code:

public void main(String text) {

    Object[] entities = (Object[])extractNamedEntities(text);

    Object names = entities[0];
    Object locations = entities[1];
    Object orgs = entities[2];

    for (Sting name : names) {
        System.out.println(name);
    }
}


private static Object extractNamedEntities(String text) {

    ArrayList<String> names = new ArrayList<String>();
    ArrayList<String> locations = new ArrayList<String>();
    ArrayList<String> orgs = new ArrayList<String>();

    // Then processing the text and then...

    names.add("Name1");
    names.add("Name2");
    locations.add("Loc1");
    locations.add("Loc2");
    orgs.add("Org1");

    return new Object[]{names, locations, orgs};
}

I get the following error:

Compilation failure for-each not applicable to expression type required: array or java.lang.Iterable found: java.lang.Object

Upvotes: 0

Views: 379

Answers (2)

knittl
knittl

Reputation: 265231

Java is a statically-typed language. What that means is that you can only call methods that are defined on the (static) type of your object references.

Take this for example:

String s = "hello";
Object o = s;

You have two variables s and o, both holding a reference to a string. You can call s.length() and get the string's length. But you cannot call o.length(), because o is typed as Object and the Object type does not define the length() method.

Define the proper types and you should be fine. Do you see that cast to an Object array when storing the return value of the method? You can get rid of that too if you declare your method to return an Object array directly. Or even better yet, declare it with the actual type that is being returned: List<String>[] (or change the code to return a list instead of an array: List<List<String>>)

Upvotes: 1

DEV
DEV

Reputation: 1726

Try this :

the public void main(String text) must be public static void main(String text[]) and the methode signature was also wrong change private static Object extractNamedEntities(String text) to extractNamedEntities(String text[])

import java.util.ArrayList;

public class Test{

    public static void main(String text[]) {

        Object[] entities = (Object[]) extractNamedEntities(text);

        Object names = entities[0];
        Object locations = entities[1];
        Object orgs = entities[2];
        System.out.println(names);
        System.out.println(locations);
        System.out.println(orgs);
    }


    private static Object extractNamedEntities(String text[]) {

        ArrayList < String > names = new ArrayList < String > ();
        ArrayList < String > locations = new ArrayList < String > ();
        ArrayList < String > orgs = new ArrayList < String > ();

        // Then processing the text and then...

        names.add("Name1");
        names.add("Name2");
        locations.add("Loc1");
        locations.add("Loc2");
        orgs.add("Org1");

        return new Object[] {
            names,
            locations,
            orgs
        };
    }
}

Upvotes: 1

Related Questions