简体   繁体   中英

Short hand (1 line statement) to initialize array of generics

I was wondering, is there any short hand to initialize array of generic?

Currently, the way to initialize array of non generics is as follow.

String[] s = {
    "hello",
    "world"
};

However, it is not possible to perform the similar, for generics.

Call<FundamentalResponse> calls[] = {
    yearlyFundamentalResponseCall,
    quarterlyFundamentalResponseCall
};

Right now, I have to do it in several lines of code.

Call<FundamentalResponse> calls[] =  (Call<FundamentalResponse>[]) new Call[2];
calls[0] = yearlyFundamentalResponseCall;
calls[1] = quarterlyFundamentalResponseCall;

I was wondering, is there any one line statement to achieve same purpose?

This should work:

Call<FundamentalResponse>[] calls = (Call<FundamentalResponse>[]) new Call[] {
    yearlyFundamentalResponseCall,
    quarterlyFundamentalResponseCall
};

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