0

I am trying to cast methods from a cloned array. However, I'm experiencing some issues. Here's the code:

    Array1[] a = new Array1[] {
        "new Array1("exampletext", 0.5, 205)
    };

    Object[] o = a.clone();
    System.out.println(o[0].getSomething());

It works when I use the "original" object. I thought the cloned object was an exact copy. Can anyone tell me whats going wrong there or give me some hints?

Edit: Okay, here's the real code ( I can't compile it as well tho! )

Drinks[] gtrk = new Drinks[] {

    new Drinks("Drink1",            0.90,      205,        0.5 ),
    new Drinks("Drink2",            0.90,      190,        0.5 ),
    new Drinks("Drink3",            0.70,      150,        0.5 ),
    new Drinks("Drink4",            20.50,     500,        2.0 ),
    new Drinks("Drink5",            28.70,     360,        1.5 ),
    new Drinks("Drink6",            6.90,      2500,       12.0 )

};

public void objectArrayTest() {

    Object[] o = gtrk.clone();
    System.out.println(o.getName());

}
FRules
  • 739
  • 1
  • 10
  • 20
  • 2
    this even won't compile cause `o` is Object[] so, object[0] is an `Object` and Object not have that method. – nachokk Jun 15 '13 at 16:46
  • 3
    There's no cast in there. The code you've given can't compile (it contains an odd number of ", for a start) and you haven't told us what Array1 is. Try posting real code. – Alan Stokes Jun 15 '13 at 16:46
  • You need reflection for that; or you need compilable code and a system compiler. – fge Jun 15 '13 at 16:48
  • A `Drinks[]` is not an `Object[]`. And an array does not have a `getName()` method. – Sotirios Delimanolis Jun 15 '13 at 17:10
  • The getName() method is defined in another class. I thought Object would be the same as an array. Thanks for the help though, I'm gonna find a better solution then! – FRules Jun 15 '13 at 17:13

1 Answers1

0

Try with this code (its works perfectly) check this LINK:

class Drinks{
String name;
int i;
double d1,d2;

public Drinks(String name, double d1, int i,  double d2) {
    super();
    this.name = name;
    this.i = i;
    this.d1 = d1;
    this.d2 = d2;
}

@Override
public String toString() {
    return "Drinks [name=" + name + ", i=" + i + ", d1=" + d1 + ", d2=" + d2 + 

"]";
    }
}

public class ArrayEg {
    public static void main(String[] args) {
        Drinks[] gtrk = new Drinks[] {
                new Drinks("Drink1",            0.90,      205,        0.5 ),
                new Drinks("Drink2",            0.90,      190,        0.5 ),
                new Drinks("Drink3",            0.70,      150,        0.5 ),
                new Drinks("Drink4",            20.50,     500,        2.0 ),
                new Drinks("Drink5",            28.70,     360,        1.5 ),
                new Drinks("Drink6",            6.90,      2500,       12.0 )
            };
        Object[] obj = new Object[gtrk.length];
        System.arraycopy(gtrk, 0, obj , 0, gtrk.length);
        for(Object o:obj){
            System.out.print((Drinks)o);    
        }
    }
}

In the case if you need call a some method from Drinks object, try with (inside the loop)

((Drinks)o).method
Community
  • 1
  • 1
Gaston Flores
  • 2,457
  • 3
  • 23
  • 42