0
public class HelloWorld{

     public static void main(String []args){
        System.out.println("Hello World");
        String s = new String("abc");
        int [] a = {1,2,3,4};
        System.out.println(s);
        System.out.println(a[1]);
        Object o1 = s;
        Object o2 = a;
        System.out.println(o1);
        System.out.println(o2[2]);

     }
}

I am geeting the following error after compiling and executing the aforementioned code on the website http://www.compileonline.com/compile_java_online.php :

Compiling the source code....
$javac HelloWorld.java 2>&1

HelloWorld.java:12: error: array required, but Object found
        System.out.println(o2[2]);
                             ^
1 error

How do I print each element of the array saved in the object o2?

fidgetyphi
  • 928
  • 1
  • 8
  • 17

4 Answers4

4

You need to cast it to int array first and print the index of it.

sample:

System.out.println(((int[])o2)[2]);

result:

3
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
2

In Java, every array is also an Object.

But every non-primitive value is an Object, so clearly not every Object is an array. It could be a String, an Integer, an object of your own class, etc. The compiler doesn't know about this at compile time: it only becomes clear at runtime what the actual type of the Object is.

You can check if your object is an array using instanceof and you can cast it to an array type:

if (o2 instanceof int[]) {
    int[] myarray = (int[])o2;
    System.out.println(myarray[2]);
}

But you need to cast it before you can use it as an array, it's not optional.

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
0

Here's the solution use following code:

public class HelloWorld{

 public static void main(String []args){
    System.out.println("Hello World");
    String s = new String("abc");
    int [] a = {1,2,3,4};
    System.out.println(s);
    System.out.println(a[1]);
    Object o1 = s;
    Object o2 = a;
    System.out.println(o1);
    System.out.println(((int[])o2)[2]);

 }

}

Output

Hello World
abc
2
abc
3

Cause: When you assign a array to object, for traversing you need to cast it back to array and than get the values

Darshan Lila
  • 5,772
  • 2
  • 24
  • 34
0

You can write a wrapper method to print the Object. This method will check the type and print the object accordingly. Like:

public class HelloWorld {

    public static void main(String []args){
        System.out.println("Hello World");
        String s = new String("abc");
        int [] a = {1,2,3,4};
        System.out.println(s);
        System.out.println(a[1]);
        Object o1 = s;
        Object o2 = a;
        printObject(o1);
        printObject(o2);
    }

    private static void printObject(Object object) {
        if (object instanceof String) {
            System.out.println(object);
        } else if (object instanceof int[]) {
            for (int element : (int []) object) {
                System.out.println(element);
            }
        }
        // Add other datatype checks
    }
}

Output will be:

Hello World
abc
2
abc
1
2
3
4

As you want to print all the elements of the object, this method do the same.

You can also check the this link to know about object type verification.

Community
  • 1
  • 1
Ambrish
  • 3,627
  • 2
  • 27
  • 42