0

I'm writing a simple Java code for competitors of some random competition. I am adding variables such as age, category, and scores. If I add integers or Strings, there is no problem, but when wanting to add int array (it works when instantiating it and write into the constructor) as an argument to the class, it does not work and thins about the array as 5 discrete integers. What am I doing wrong?

private int [] danceScores = new int[5]; //instance variable

public Competitor(int CNumber, String cName, String cLevel, String dStyle, int[] cScores){                          
    ...;
    danceScores = cScores;}

// in Main method
Competitor competitor1= new Competitor(1, "name", "level", "dance", {1,2,3,4,5});
mrakoplas
  • 94
  • 8

3 Answers3

1

Passing integer array is not formatted. Syntax is wrong here.

It should be like below

Competitor competitor1= new Competitor(1, "name", "level", "dance", new int[] {1,2,3,4,5});
George Weekson
  • 483
  • 3
  • 13
0

you have to pass it like this

Competitor competitor1= new Competitor(1, "name", "level", "dance", new int[]{1,2,3,4,5});
George Weekson
  • 483
  • 3
  • 13
0

The above constructor call should have given compilation error stating "Array initializer is not allowed here". Add new int[]

Competitor competitor1= new Competitor(1, "name", "level", "dance", new int[] {1,2,3,4,5});

Sachin
  • 2,087
  • 16
  • 22