Returning an array without assign to a variable

前端 未结 5 1718
南方客
南方客 2021-01-30 20:24

Is there any way in java to return a new array without assigning it first to a variable? Here is an example:

public class Data {
    private int a;
    private i         


        
5条回答
  •  不思量自难忘°
    2021-01-30 21:06

    You still need to create the array, even if you do not assign it to a variable. Try this:

    public int[] getData() {
        return new int[] {a,b,c,d};
    }
    

    Your code sample did not work because the compiler, for one thing, still needs to know what type you are attempting to create via static initialization {}.

提交回复
热议问题