6

It sounds easy but i've been trying to do it for quite sometime, I want to initialize my custom class object array using curly braces

Here is the failed example:

class:

class Tranforminfo{
        int left;
        int top;
        int right;
        int bottom;
        float rorate;

        public Tranforminfo(int left, int top, int right, int bottom, float rorate) {
            this.left = left;
            this.top = top;
            this.right = right;
            this.bottom = bottom;
            this.rorate = rorate;
        }
    }

Usage: (not correct)

// attempt 1 
Tranforminfo somedamn = new Tranforminfo[]{(1,2,3,4,5),(6,4,3,5,6)};

// attempt 2
Tranforminfo somedamn = new Tranforminfo[]{{1,2,3,4,5},{6,4,3,5,6}};

// attempt 3
Tranforminfo somedamn = new Tranforminfo[]((1,2,3,4,5),(6,4,3,5,6));

No luck so far help appreciated , i am coding in android(JAVA)

Zulqurnain Jutt
  • 1,083
  • 3
  • 15
  • 41
  • Read our lovely new Docs page http://stackoverflow.com/documentation/java/99/arrays/404/creating-and-initializing-arrays#t=201608141953434615501... – Tunaki Aug 14 '16 at 19:54
  • attempt 1 is the closest: `Tranforminfo[] somedamn = new Tranforminfo[]{new Tranforminfo(1,2,3,4,5), new Tranforminfo(6,4,3,5,6)};`. (But you can drop the `new Tranforminfo[]` too). – Andy Turner Aug 14 '16 at 19:55
  • i have to write new for each one ? that's strange C/C++ would have allowed this – Zulqurnain Jutt Aug 14 '16 at 19:56
  • Java and C/C++ are not the same language; don't expect the same features. – Andy Turner Aug 14 '16 at 19:57
  • @Mr.Z this looks like Java, to me, not like C++ – VLAZ Aug 14 '16 at 19:58
  • well definitely if cliff `height=0` , thanks anyway – Zulqurnain Jutt Aug 14 '16 at 19:58
  • Note that an array of type `A[]` can also contain objects that are a *subclass* of `A`. You can even create an array with an *interface* as element type (e.g. `Runnable[]`). While you could argue it'd be nice to have a short-hand syntax *for your case*, it doesn't extend very well to general cases. – Mattias Buelens Aug 14 '16 at 20:04
  • This is well covered in this question http://stackoverflow.com/questions/5364278/creating-an-array-of-objects-in-java – Tunaki Aug 14 '16 at 20:06

2 Answers2

10

There is some ways to do it:

Transforminfo[] somedamn = 
    new Transforminfo[] { new Transforminfo(1,2,3,4,5),
                          new Transforminfo(6,4,3,5,6) };
Transforminfo[] somedamn = 
    { new Transforminfo(1,2,3,4,5), new Transforminfo(6,4,3,5,6) };  

First you create Transforminfo array link, then add new Transforminfo elements into.

It's like Integer []array = {1, 2, 3}, but you have to use constructor to create Transforminfo elements.

One more example to understand array of object creating. All way is equal.

String array[] = { new String("str1"), new String("str2") };
String[] array = { new String("str1"), new String("str2") };
String array[] = new String[] { new String("str1"), new String("str2") };
String[] array = new String[] { new String("str1"), new String("str2") };
jisecayeyo
  • 250
  • 1
  • 9
  • Strings are really odd beasts to use as an example here. `... { new String("str1"), ...` as in your examples makes a new object in the heap but `String[] array = { "str1", "str2" };` would use the [string constant pool](http://stackoverflow.com/a/3052456/6178459) – Arjan Aug 15 '16 at 03:26
3
Transforminfo[] somedamn = {new Transforminfo(1,2,3,4,5), new Transforminfo(1,2,3,4,5)};

Transforminfo[] is creating a link to an Array of Transforminfo and with the {...} you create the actual Array (a special java syntax and actually the shortest one)

What you did was: You created a link to a Transforminfo Object and tried to set this link to an Array Object

Regedit
  • 31
  • 5