Pergunta

I need to create anonymous int array to be passed to a method. I don't want to declare a variable and then pass the variable because it makes code look crowded. This is what we can do for Integer array.

new Box(new Integer[]{1,2,3});

how to do that with int?

Foi útil?

Solução

Just do:

new Box(new int[]{1,2,3});

Outras dicas

If you already know about array contents, you can always employ the following methodology

public void foo(int... args) {
    for (int arg : args) {
      // do smth with arg.
     }
}


foo(1,2,3,4,5,6,7); //pretty much unlimited arguments can be passed in.


//or you can do the same to a class constructor as well
new Box(1,2,3,4,5,6);

class Box {
  public Box(int... args){
    //loop 
  }
}

Hope it helps

ok. it seems its same as the question.

new Box(new int[]{1,2,3});

not sure why eclipse gave me the squiggly red bar before.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top