-3

My program requires me to keep count of an audience and add their number into an array.

count increases by one every time the user purchases a ticket. I can't find the way to initialize the values here.

count = 9;
long [] audienceInfo = new long[count];
audienceInfo[count] = { 1, 2, 3, 8, 9, 10, 15, 16, 17, 12 };

The error I'm receiving is "Array constants can only be used in initializers" Any help is appreciated. Thank you!

Edit: Thanks for the help. I used ArrayList and it solved my problems

laft
  • 1
  • 1
  • The numbers "1", "2", "3", "8" and so on are what? Counts? Seat numbers? Or what? – tquadrat Mar 30 '20 at 11:44
  • Arrays in Java have a **fixed size** for an "array" that's **dynamically sized** use an `ArrayList` – Lino Mar 30 '20 at 11:44
  • `count` is 9 but you have 10 values. Are you asking how to initialize an array with 10 values, or are you asking how to grow an array in size? Answer to first is `long[] audienceInfo = { 1, 2, 3, 8, 9, 10, 15, 16, 17, 12 }`. Answer to second is you can't and that we'd recommend you use a `List` instead. – Andreas Mar 30 '20 at 11:45
  • And for the error message: only `long [] audienceInfo = new long [] { 1, 2, 3, 8, 9, 10, 15, 16, 17, 12 };` would work. – tquadrat Mar 30 '20 at 11:45
  • 1
    @tquadrat You don't need to `new long []` part. – Andreas Mar 30 '20 at 11:46

1 Answers1

-2

You can only assign this value when you are initializing the array, like so:

long [] audienceInfo = new long[] { 1, 2, 3, 8, 9, 10, 15, 16, 17, 12 };
Lazar Petrovic
  • 537
  • 3
  • 8